From: Mikko Keinänen Date: Fri, 10 Dec 2010 23:22:39 +0000 (+0200) Subject: Created a new delegate (FileSystemBrowseDelegate) and a new widget X-Git-Url: http://git.maemo.org/git/?p=emufront;a=commitdiff_plain;h=b7d8da5544298be9317d0630b0a0ed9083051a44 Created a new delegate (FileSystemBrowseDelegate) and a new widget used in the delegate (FilePathSelectorWidget). Used the new delegate in FilePathEditView. --- diff --git a/src/delegates/filesystembrowsedelegate.cpp b/src/delegates/filesystembrowsedelegate.cpp new file mode 100644 index 0000000..336aa08 --- /dev/null +++ b/src/delegates/filesystembrowsedelegate.cpp @@ -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 . + +#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(sender()); + emit commitData(editor); + emit closeEditor(editor); +} + +void FileSystemBrowseDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const +{ + FilePathSelectorWidget *wdg = qobject_cast(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(editor); + fpwdg->setFilePath(path); +} diff --git a/src/delegates/filesystembrowsedelegate.h b/src/delegates/filesystembrowsedelegate.h new file mode 100644 index 0000000..ca17536 --- /dev/null +++ b/src/delegates/filesystembrowsedelegate.h @@ -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 . + +#ifndef FILESYSTEMBROWSEDELEGATE_H +#define FILESYSTEMBROWSEDELEGATE_H + +#include + +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 diff --git a/src/emufront.pro b/src/emufront.pro index aef4bef..a6ca1dc 100644 --- a/src/emufront.pro +++ b/src/emufront.pro @@ -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 diff --git a/src/views/filepatheditview.cpp b/src/views/filepatheditview.cpp index 19e4194..36b1069 100644 --- a/src/views/filepatheditview.cpp +++ b/src/views/filepatheditview.cpp @@ -21,6 +21,7 @@ #include "filepathmodel.h" #include "setupmodel.h" #include "comboboxdelegate.h" +#include "filesystembrowsedelegate.h" #include 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 index 0000000..f6acd5c --- /dev/null +++ b/src/widgets/filepathselectorwidget.cpp @@ -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 . + +#include +#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 index 0000000..a5f862f --- /dev/null +++ b/src/widgets/filepathselectorwidget.h @@ -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 . + +#ifndef FILEPATHSELECTORWIDGET_H +#define FILEPATHSELECTORWIDGET_H + +#include + +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