Source

ggap / ggap / prefsdialog-p.h

#ifndef GGAP_PREFS_DIALOG_P_H
#define GGAP_PREFS_DIALOG_P_H

#include "ui_editfilelist.h"
#include "ui_prefs-advanced.h"
#include <ggap/prefsdialog.h>
#include <ggap/prefs.h>
#include <moo/macros.h>
#include <QFileDialog>
#include <QStringList>
#include <QStringListModel>
#include <QPointer>

namespace ggap {

class EditFileListDialog : public QDialog, private Ui::EditFileListDialog {
    Q_OBJECT

    QStringListModel model;
    bool choose_dirs;

public:
    EditFileListDialog(const QStringList &dirs, bool choose_dirs,
                       const QString &title, QWidget *parent) :
        QDialog(parent),
        model(dirs),
        choose_dirs(choose_dirs)
    {
        setWindowTitle(title);
        setupUi(this);
        dirList->setModel(&model);
        if (!dirs.isEmpty())
            selectRow(dirs.size() - 1);
        updateSensitivity();
        connect(dirList->selectionModel(),
                SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),
                SLOT(updateSensitivity()));
    }

    QStringList dirs() const
    {
        return model.stringList();
    }

private:
    void selectRow(const QModelIndex &idx)
    {
        dirList->selectionModel()->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect);
    }

    void selectRow(int row)
    {
        dirList->selectionModel()->setCurrentIndex(model.index(row), QItemSelectionModel::ClearAndSelect);
    }

    QModelIndex selectedRow() const
    {
        return dirList->selectionModel()->currentIndex();
    }

    QStringList getFiles(bool single, const QString &start = QString())
    {
        if (!choose_dirs && !single)
            return QFileDialog::getOpenFileNames(this, tr("Choose Files"), start);

        QStringList ret;
        QString d;

        if (choose_dirs)
            d = QFileDialog::getExistingDirectory(this, tr("Choose Directory"), start);
        else
            d = QFileDialog::getOpenFileName(this, tr("Choose File"), start);

        if (!d.isEmpty())
            ret << d;

        return ret;
    }

    void editIndex(const QModelIndex &idx)
    {
        m_return_if_fail(idx.isValid());
        QString old_path = model.data(idx, Qt::DisplayRole).toString();
        QStringList new_paths = getFiles(true, old_path);
        if (!new_paths.isEmpty())
            model.setData(idx, new_paths[0]);
    }

private Q_SLOTS:
    void updateSensitivity()
    {
        QModelIndex idx = selectedRow();
        buttonRemove->setEnabled(idx.isValid());
        buttonOpen->setEnabled(idx.isValid());
        buttonUp->setEnabled(idx.isValid() && idx.row() > 0);
        buttonDown->setEnabled(idx.isValid() && idx.row() + 1 < model.rowCount());
    }

    void on_dirList_activated(const QModelIndex &idx)
    {
        editIndex(idx);
    }

    void on_buttonOpen_clicked()
    {
        QModelIndex idx = selectedRow();
        m_return_if_fail(idx.isValid());
        editIndex(idx);
    }

    void on_buttonAdd_clicked()
    {
        QStringList paths = getFiles(false);
        if (paths.isEmpty())
            return;

        int row = model.rowCount();
        QModelIndex idx = selectedRow();
        if (idx.isValid())
            row = idx.row() + 1;

        foreach (const QString &p, paths)
        {
            model.insertRows(row, 1);
            idx = model.index(row);
            model.setData(idx, p);
            selectRow(idx);
            row += 1;
        }

        updateSensitivity();
    }

    void on_buttonRemove_clicked()
    {
        QModelIndex idx = selectedRow();
        m_return_if_fail(idx.isValid());
        model.removeRows(idx.row(), 1);
        updateSensitivity();
    }

    void on_buttonUp_clicked()
    {
        QModelIndex idx = selectedRow();
        if (idx.isValid() && idx.row() != 0)
        {
            int row = idx.row();
            QString path = model.data(idx, Qt::DisplayRole).toString();
            model.removeRows(row, 1);
            model.insertRows(row - 1, 1);
            idx = model.index(row - 1);
            model.setData(idx, path);
            selectRow(idx);
        }
    }

    void on_buttonDown_clicked()
    {
        QModelIndex idx = selectedRow();
        if (idx.isValid() && idx.row() != model.rowCount() - 1)
        {
            int row = idx.row();
            QString path = model.data(idx, Qt::DisplayRole).toString();
            model.removeRows(row, 1);
            model.insertRows(row + 1, 1);
            idx = model.index(row + 1);
            model.setData(idx, path);
            selectRow(idx);
        }
    }
};

class AdvancedPrefsDialog : public PrefsDialogBase, private Ui::AdvancedPrefsDialog {
    Q_OBJECT

    static QPointer<AdvancedPrefsDialog> instance;
    QStringList files;

    void init()
    {
        checkSaveWorkspace->setChecked(prefsValue(Prefs::SaveWorkspace));
        files = prefsValue(Prefs::WatchGapFiles);
        entryFiles->setText(files.join("; "));

        checkEnableTerminalMode->setChecked(prefsValue(Prefs::EnableTerminalMode));
        checkGap44->setChecked(prefsValue(Prefs::Gap44));

        if (prefsValue(Prefs::DefaultFileFormat) == "workspace")
            radioWorkspace->setChecked(true);
        else
            radioWorksheet->setChecked(true);
    }

    void apply()
    {
        setPrefsValue(Prefs::SaveWorkspace, checkSaveWorkspace->isChecked());
        setPrefsValue(Prefs::WatchGapFiles, files);

        setPrefsValue(Prefs::EnableTerminalMode, checkEnableTerminalMode->isChecked());
        setPrefsValue(Prefs::Gap44, checkGap44->isChecked());

        if (radioWorkspace->isChecked())
            setPrefsValue(Prefs::DefaultFileFormat, "workspace");
        else
            setPrefsValue(Prefs::DefaultFileFormat, "worksheet");
    }

public:
    AdvancedPrefsDialog(QWidget *parent) :
        PrefsDialogBase(parent)
    {
        setupUi(this);
        QButtonGroup *group = new QButtonGroup(this);
        group->addButton(radioWorkspace);
        group->addButton(radioWorksheet);
        init();
    }

    static void showDialog(QWidget *parent)
    {
        NiceDialog::showDialog(instance, parent);
    }

private Q_SLOTS:
    void on_buttonFiles_clicked()
    {
        EditFileListDialog dlg(files, false, tr("Choose Files"), this);

        if (dlg.exec())
        {
            files = dlg.dirs();
            entryFiles->setText(files.join("; "));
        }
    }
};

}

#endif // GGAP_PREFS_DIALOG_P_H