Created a new delegate (FileSystemBrowseDelegate) and a new widget
authorMikko Keinänen <mikko.keinanen@gmail.com>
Fri, 10 Dec 2010 23:22:39 +0000 (01:22 +0200)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Fri, 10 Dec 2010 23:22:39 +0000 (01:22 +0200)
used in the delegate (FilePathSelectorWidget). Used the new
delegate in FilePathEditView.

src/delegates/filesystembrowsedelegate.cpp [new file with mode: 0644]
src/delegates/filesystembrowsedelegate.h [new file with mode: 0644]
src/emufront.pro
src/views/filepatheditview.cpp
src/widgets/filepathselectorwidget.cpp [new file with mode: 0644]
src/widgets/filepathselectorwidget.h [new file with mode: 0644]

diff --git a/src/delegates/filesystembrowsedelegate.cpp b/src/delegates/filesystembrowsedelegate.cpp
new file mode 100644 (file)
index 0000000..336aa08
--- /dev/null
@@ -0,0 +1,54 @@
+// EmuFront
+// Copyright 2010 Mikko Keinänen
+//
+// This file is part of EmuFront.
+//
+//
+// EmuFront is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
+//
+// EmuFront is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+
+#include "filesystembrowsedelegate.h"
+#include "filepathselectorwidget.h"
+
+FileSystemBrowseDelegate::FileSystemBrowseDelegate(QWidget *parent) :
+    QStyledItemDelegate(parent)
+{
+}
+
+QWidget* FileSystemBrowseDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+    FilePathSelectorWidget *editor = new FilePathSelectorWidget(parent);
+    connect(editor, SIGNAL(filePathUpdated()), this, SLOT(commitAndCloseEditor()));
+    return editor;
+}
+
+void FileSystemBrowseDelegate::commitAndCloseEditor()
+{
+    FilePathSelectorWidget *editor = qobject_cast<FilePathSelectorWidget *>(sender());
+    emit commitData(editor);
+    emit closeEditor(editor);
+}
+
+void FileSystemBrowseDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
+{
+    FilePathSelectorWidget *wdg = qobject_cast<FilePathSelectorWidget *>(editor);
+    QString path = wdg->getFilePath();
+    model->setData(index, path);
+}
+
+void FileSystemBrowseDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
+{
+    QString path = index.model()->data(index, Qt::DisplayRole).toString();
+    FilePathSelectorWidget *fpwdg = qobject_cast<FilePathSelectorWidget *>(editor);
+    fpwdg->setFilePath(path);
+}
diff --git a/src/delegates/filesystembrowsedelegate.h b/src/delegates/filesystembrowsedelegate.h
new file mode 100644 (file)
index 0000000..ca17536
--- /dev/null
@@ -0,0 +1,39 @@
+// EmuFront
+// Copyright 2010 Mikko Keinänen
+//
+// This file is part of EmuFront.
+//
+//
+// EmuFront is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
+//
+// EmuFront is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef FILESYSTEMBROWSEDELEGATE_H
+#define FILESYSTEMBROWSEDELEGATE_H
+
+#include <QStyledItemDelegate>
+
+class FileSystemBrowseDelegate : public QStyledItemDelegate
+{
+    Q_OBJECT
+public:
+    explicit FileSystemBrowseDelegate(QWidget *parent = 0);
+    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
+    void setEditorData(QWidget *editor, const QModelIndex &index) const;
+    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
+
+private slots:
+    void commitAndCloseEditor();
+
+};
+
+#endif // FILESYSTEMBROWSEDELEGATE_H
index aef4bef..a6ca1dc 100644 (file)
@@ -96,7 +96,9 @@ HEADERS += mainwindow.h \
     delegates/comboboxdelegate.h \
     delegates/stringlistdelegate.h \
     models/filepathmodel.h \
-    views/filepatheditview.h
+    views/filepatheditview.h \
+    delegates/filesystembrowsedelegate.h \
+    widgets/filepathselectorwidget.h
 SOURCES += main.cpp \
     mainwindow.cpp \
     db/databasemanager.cpp \
@@ -168,7 +170,9 @@ SOURCES += main.cpp \
     delegates/comboboxdelegate.cpp \
     delegates/stringlistdelegate.cpp \
     models/filepathmodel.cpp \
-    views/filepatheditview.cpp
+    views/filepatheditview.cpp \
+    delegates/filesystembrowsedelegate.cpp \
+    widgets/filepathselectorwidget.cpp
 OTHER_FILES +=  
 
 CONFIG += mobility
index 19e4194..36b1069 100644 (file)
@@ -21,6 +21,7 @@
 #include "filepathmodel.h"
 #include "setupmodel.h"
 #include "comboboxdelegate.h"
+#include "filesystembrowsedelegate.h"
 #include <QtGui>
 
 FilePathEditView::FilePathEditView(QWidget *parent) :
@@ -36,5 +37,7 @@ FilePathEditView::FilePathEditView(QWidget *parent) :
         this
     );
     objectList->setItemDelegateForColumn(FilePathModel::FilePath_SetupId, setupDelegate);
+    FileSystemBrowseDelegate *fsBrowseDelegate = new FileSystemBrowseDelegate(this);
+    objectList->setItemDelegateForColumn(FilePathModel::FilePath_Name, fsBrowseDelegate);
     postInit();
 }
diff --git a/src/widgets/filepathselectorwidget.cpp b/src/widgets/filepathselectorwidget.cpp
new file mode 100644 (file)
index 0000000..f6acd5c
--- /dev/null
@@ -0,0 +1,66 @@
+// EmuFront
+// Copyright 2010 Mikko Keinänen
+//
+// This file is part of EmuFront.
+//
+//
+// EmuFront is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
+//
+// EmuFront is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <QtGui>
+#include "filepathselectorwidget.h"
+
+FilePathSelectorWidget::FilePathSelectorWidget(QWidget *parent) :
+    QWidget(parent)
+{
+    filePathLabel = new QLabel;
+    filePathButton = new QPushButton(tr("&Browse"));
+    QHBoxLayout *layout = new QHBoxLayout;
+    layout->addWidget(filePathButton);
+    layout->addWidget(filePathLabel);
+    setLayout(layout);
+    connectSignals();
+}
+
+void FilePathSelectorWidget::connectSignals()
+{
+    connect(filePathButton, SIGNAL(clicked()), this, SLOT(browseFilePath()));
+}
+
+void FilePathSelectorWidget::browseFilePath()
+{
+    qDebug() << "FilePathSelectorWidget::browseFilePath()";
+    QString startPath = filePathLabel->text().isEmpty() ?
+        QDir::homePath() : filePathLabel->text();
+
+    QString fpath = QFileDialog::getExistingDirectory(this,
+        tr("Select a directory"), startPath,
+        QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
+            QDir d(fpath);
+
+    if (d.exists() && d.isReadable()) {
+        filePathLabel->setText(d.path());
+        if (d.path() != startPath)
+            emit filePathUpdated();
+    }
+}
+
+void FilePathSelectorWidget::setFilePath(QString &path)
+{
+    filePathLabel->setText(path);
+}
+
+QString FilePathSelectorWidget::getFilePath() const
+{
+    return filePathLabel->text();
+}
diff --git a/src/widgets/filepathselectorwidget.h b/src/widgets/filepathselectorwidget.h
new file mode 100644 (file)
index 0000000..a5f862f
--- /dev/null
@@ -0,0 +1,48 @@
+// EmuFront
+// Copyright 2010 Mikko Keinänen
+//
+// This file is part of EmuFront.
+//
+//
+// EmuFront is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
+//
+// EmuFront is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef FILEPATHSELECTORWIDGET_H
+#define FILEPATHSELECTORWIDGET_H
+
+#include <QWidget>
+
+class QLabel;
+class QPushButton;
+
+class FilePathSelectorWidget : public QWidget
+{
+    Q_OBJECT
+public:
+    FilePathSelectorWidget(QWidget *parent = 0);
+    void setFilePath(QString &path);
+    QString getFilePath() const;
+
+signals:
+    void filePathUpdated();
+
+protected slots:
+    void browseFilePath();
+
+protected:
+    void connectSignals();
+    QLabel *filePathLabel;
+    QPushButton *filePathButton;
+};
+
+#endif // FILEPATHSELECTORWIDGET_H