comparison src/folderselectdialog.cpp @ 9:e3c8f61e45a9

Implement folderselection dialogs
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 26 Mar 2015 16:30:18 +0100
parents
children 971bd39a6116
comparison
equal deleted inserted replaced
8:ac4db84f1d9d 9:e3c8f61e45a9
1 /* Copyright (C) 2014 by Intevation GmbH
2 *
3 * This file is Free Software under the GNU GPL (v>=2)
4 * and comes with ABSOLUTELY NO WARRANTY!
5 * See LICENSE.txt for details.
6 */
7 #include "folderselectdialog.h"
8 #include "constants.h"
9
10 #include <QSortFilterProxyModel>
11 #include <QStandardItemModel>
12 #include <QTableView>
13 #include <QVBoxLayout>
14 #include <QLineEdit>
15 #include <QLabel>
16 #include <QApplication>
17 #include <QStyle>
18 #include <QFileDialog>
19 #include <QStandardPaths>
20 #include <QPushButton>
21 #include <QHeaderView>
22 #include <QDebug>
23 #include <QSettings>
24 #include <QStringList>
25 #include <QStandardItem>
26
27 FolderSelectDialog::FolderSelectDialog(const QString& startFolder,
28 const QString& folderPattern,
29 const QString& pathLabel,
30 QWidget * parent,
31 Qt::WindowFlags f) :
32 QDialog(parent, f),
33 mCurFolder(startFolder),
34 mFolderPattern(folderPattern),
35 mPathLabelString(pathLabel),
36 mPathLabel(NULL),
37 mPathLineEdit(NULL)
38 {
39 mSortModel = new QSortFilterProxyModel();
40 mModel = new QStandardItemModel();
41 mShowPathSelection = !pathLabel.isEmpty();
42 setupGUI();
43 if (mShowPathSelection) {
44 mPathLabel->setText("<b>" + pathLabel + ":</b> ");
45 }
46 setFolder(mCurFolder);
47 }
48
49 void FolderSelectDialog::setupGUI() {
50 QVBoxLayout *base = new QVBoxLayout;
51 QHBoxLayout *folderChangeArea = new QHBoxLayout;
52
53 if (mShowPathSelection) {
54 mPathLabel = new QLabel;
55 folderChangeArea->addWidget(mPathLabel);
56 base->addLayout(folderChangeArea);
57
58 QPushButton *folderSelect = new QPushButton();
59 folderSelect->setToolTip(tr("Select folder"));
60 folderSelect->setIcon(QApplication::style()->standardIcon(QStyle::SP_DirIcon));
61 connect(folderSelect, &QPushButton::clicked, this,
62 &FolderSelectDialog::changeFolderClicked);
63 folderChangeArea->addWidget(folderSelect);
64
65 mPathLineEdit = new QLineEdit;
66 mPathLineEdit->setClearButtonEnabled(true);
67 connect(mPathLineEdit, &QLineEdit::textChanged, this,
68 &FolderSelectDialog::pathLineChanged);
69 folderChangeArea->addWidget(mPathLineEdit);
70
71 mGoButton = new QPushButton(tr("Go"));
72 mGoButton->setEnabled(false);
73 folderChangeArea->addWidget(mGoButton);
74 connect(mGoButton, &QPushButton::clicked, this,
75 &FolderSelectDialog::goClicked);
76 }
77
78 mFilterWidget = new FilterWidget(mSortModel);
79 base->addWidget(mFilterWidget);
80
81 mView = new QTableView;
82 mView->setModel(mSortModel);
83
84 mView->resizeColumnsToContents();
85 mView->setSelectionBehavior(QAbstractItemView::SelectRows);
86 mView->setSelectionMode(QAbstractItemView::SingleSelection);
87 mView->setSortingEnabled(true);
88 mView->setEditTriggers(QAbstractItemView::NoEditTriggers);
89 connect(mView->selectionModel(), &QItemSelectionModel::selectionChanged,
90 this, &FolderSelectDialog::selectionChanged);
91 connect(mView, &QTableView::doubleClicked, this,
92 &FolderSelectDialog::wantToAccept);
93
94 base->addWidget(mView);
95
96 QHBoxLayout * bottomButtons = new QHBoxLayout;
97 base->addLayout(bottomButtons);
98
99 mOkButton = new QPushButton(tr("Ok"));
100 mOkButton->setEnabled(false);
101 connect(mOkButton, &QPushButton::clicked, this,
102 &FolderSelectDialog::wantToAccept);
103 bottomButtons->addStretch(-1);
104 bottomButtons->addWidget(mOkButton);
105 setLayout(base);
106 }
107
108 void FolderSelectDialog::selectionChanged (const QItemSelection& selected,
109 const QItemSelection& deselected) {
110 mOkButton->setEnabled(!selected.indexes().isEmpty());
111 }
112
113 void FolderSelectDialog::pathLineChanged() {
114 const QString path = mPathLineEdit->text();
115 qDebug() << "path: " << path;
116 if (path.isEmpty()) {
117 mGoButton->setEnabled(false);
118 return;
119 }
120 QDir dir(path);
121 if (dir.exists()) {
122 mGoButton->setEnabled(true);
123 return;
124 }
125 mGoButton->setEnabled(false);
126 }
127
128
129 void FolderSelectDialog::goClicked() {
130 setFolder(mPathLineEdit->text());
131 QSettings settings;
132 /* assuming go is only available in root folder mode */
133 settings.setValue(ROOT_FOLDER_KEY, mPathLineEdit->text());
134 }
135
136 void FolderSelectDialog::setFolder(const QString& folder) {
137 mCurFolder = folder;
138 if (mPathLineEdit) {
139 mPathLineEdit->setText(mCurFolder);
140 }
141
142 QStringList columns = mFolderPattern.split(PATTERN_SEPERATOR);
143 mModel->clear();
144 mModel->setHorizontalHeaderLabels(columns);
145 QDir dir(folder);
146 qDebug() << "Folder set to: " << folder;
147 foreach (const QString & subfolder, dir.entryList(QDir::Dirs |
148 QDir::Readable |
149 QDir::NoDotAndDotDot)) {
150 qDebug() << "Looking at: " << subfolder;
151 QStringList itemData = subfolder.split(PATTERN_SEPERATOR);
152 if (itemData.size() != columns.size()) {
153 qDebug() << "Failed to parse: " << subfolder;
154 continue;
155 }
156 QList<QStandardItem*> items;
157 foreach (const QString& part, itemData) {
158 QStandardItem * item = new QStandardItem(part);
159 item->setData(dir.absoluteFilePath(subfolder));
160 items.append(item);
161 }
162 mModel->appendRow(items);
163 }
164 mSortModel->setSourceModel(mModel);
165 }
166
167 void FolderSelectDialog::wantToAccept() {
168 QItemSelectionModel *selection = mView->selectionModel();
169 QModelIndexList selected = selection->selectedIndexes();
170 if (selected.isEmpty()) { /* Ok should not be enabled in that case */
171 return;
172 }
173 const QString folder = selected[0].data(Qt::UserRole + 1).toString();
174
175 emit folderSelected(folder);
176 accept();
177 }
178
179 void FolderSelectDialog::changeFolderClicked()
180 {
181 const QString oldFolder = mCurFolder;
182 const QString startLoc = mCurFolder.isEmpty() ?
183 QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) :
184 mCurFolder;
185
186 QString outFolder = QFileDialog::getExistingDirectory(
187 this, tr("Select ") + mPathLabelString,
188 startLoc);
189 if (outFolder.isEmpty()) {
190 /* User aborted */
191 return;
192 }
193 setFolder(outFolder);
194 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)