Root/
1 | /* |
2 | * Copyright 2010-2011 Niels Kummerfeldt <niels.kummerfeldt@tu-harburg.de> |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301 USA |
18 | */ |
19 | |
20 | #include "fileselector.h" |
21 | |
22 | #include <QtCore/QDebug> |
23 | #include <QtCore/QDir> |
24 | #include <QtCore/QSettings> |
25 | #include <QtGui/QKeyEvent> |
26 | #include <QtGui/QLayout> |
27 | |
28 | FileSelector::FileSelector(QWidget *parent) |
29 | : QWidget(parent), |
30 | m_view(new QListView(this)), |
31 | m_model(new QFileSystemModel(this)), |
32 | m_title(new QLabel(this)), |
33 | m_path(new QLabel(this)), |
34 | m_bookmarkButton(new QPushButton(this)), |
35 | m_bookmarkMenu(new QMenu(this)), |
36 | m_bookmarks(), |
37 | m_signalMapper(new QSignalMapper(this)) |
38 | { |
39 | QGridLayout *layout = new QGridLayout(this); |
40 | layout->setContentsMargins(0, 0, 0, 0); |
41 | layout->setRowStretch(5, 1); |
42 | layout->setSpacing(0); |
43 | |
44 | m_title->setAlignment(Qt::AlignCenter); |
45 | layout->addWidget(m_title, 0, 0, 1, 2); |
46 | layout->addWidget(m_path, 1, 0, 1, 2); |
47 | |
48 | m_view->setModel(m_model); |
49 | connect(m_view, SIGNAL(activated(QModelIndex)), this, SLOT(enter(QModelIndex))); |
50 | layout->addWidget(m_view, 2, 0, 6, 1); |
51 | m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
52 | m_view->setSelectionMode(QListView::ExtendedSelection); |
53 | QFont font = m_view->font(); |
54 | font.setPointSize(8); |
55 | m_view->setFont(font); |
56 | |
57 | m_model->setNameFilterDisables(false); |
58 | m_model->setRootPath("/"); |
59 | |
60 | connect(m_signalMapper, SIGNAL(mapped(QString)), this, SLOT(setCurrentDirectory(QString))); |
61 | |
62 | m_bookmarkButton->setIcon(QIcon(":nobookmark.png")); |
63 | m_bookmarkButton->setShortcut(QKeySequence(Qt::ALT+Qt::Key_B)); |
64 | connect(m_bookmarkButton, SIGNAL(clicked()), this, SLOT(toggleBookmark())); |
65 | layout->addWidget(m_bookmarkButton, 2, 1); |
66 | |
67 | QPushButton *button = new QPushButton(); |
68 | button->setIcon(QIcon(":up.png")); |
69 | button->setShortcut(QKeySequence(Qt::ALT+Qt::Key_Up)); |
70 | connect(button, SIGNAL(clicked()), this, SLOT(goUp())); |
71 | layout->addWidget(button, 3, 1); |
72 | |
73 | button = new QPushButton(); |
74 | button->setIcon(QIcon(":ok.png")); |
75 | connect(button, SIGNAL(clicked()), this, SLOT(accept())); |
76 | layout->addWidget(button, 6, 1); |
77 | |
78 | button = new QPushButton(); |
79 | button->setIcon(QIcon(":cancel.png")); |
80 | button->setShortcut(QKeySequence(Qt::Key_Escape)); |
81 | connect(button, SIGNAL(clicked()), this, SIGNAL(cancel())); |
82 | layout->addWidget(button, 7, 1); |
83 | |
84 | QSettings conf(QDir::homePath()+"Maps/nanomap.conf", QSettings::NativeFormat); |
85 | conf.beginGroup("fileselector"); |
86 | m_bookmarks = conf.value("bookmarks").toStringList(); |
87 | conf.endGroup(); |
88 | |
89 | setCurrentDirectory(m_model->index(QDir::homePath())); |
90 | updateBookmarkMenu(); |
91 | |
92 | m_view->setFocus(Qt::OtherFocusReason); |
93 | resize(320, 240); |
94 | } |
95 | |
96 | FileSelector::~FileSelector() |
97 | { |
98 | QSettings conf(QDir::homePath()+"Maps/nanomap.conf", QSettings::NativeFormat); |
99 | conf.beginGroup("fileselector"); |
100 | conf.setValue("bookmarks", m_bookmarks); |
101 | conf.endGroup(); |
102 | } |
103 | |
104 | void FileSelector::setTitle(const QString &title) |
105 | { |
106 | m_title->setText("<u>"+title+"</u>"); |
107 | } |
108 | |
109 | void FileSelector::setFileTypes(const QStringList &types) |
110 | { |
111 | m_model->setNameFilters(types); |
112 | } |
113 | |
114 | void FileSelector::keyPressEvent(QKeyEvent *event) |
115 | { |
116 | if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_B) { |
117 | m_bookmarkMenu->popup(mapToGlobal(QPoint(0, 0))); |
118 | } |
119 | } |
120 | |
121 | void FileSelector::toggleBookmark() |
122 | { |
123 | QString path = m_model->filePath(m_view->rootIndex()); |
124 | if (m_bookmarks.contains(path)) { |
125 | m_bookmarks.removeAll(path); |
126 | m_bookmarkButton->setIcon(QIcon(":nobookmark.png")); |
127 | } else { |
128 | m_bookmarks.append(path); |
129 | m_bookmarkButton->setIcon(QIcon(":bookmark.png")); |
130 | } |
131 | updateBookmarkMenu(); |
132 | } |
133 | |
134 | void FileSelector::goUp() |
135 | { |
136 | QModelIndex current = m_view->rootIndex(); |
137 | if (current.isValid()) { |
138 | QModelIndex up = current.parent(); |
139 | if (up.isValid()) { |
140 | setCurrentDirectory(up); |
141 | } |
142 | } |
143 | } |
144 | |
145 | void FileSelector::accept() |
146 | { |
147 | QModelIndex index = m_view->currentIndex(); |
148 | if (index.isValid()) { |
149 | if (!m_model->isDir(index)) { |
150 | emit fileSelected(m_model->filePath(index)); |
151 | } |
152 | } |
153 | } |
154 | |
155 | void FileSelector::enter(const QModelIndex &index) |
156 | { |
157 | if (m_model->isDir(index)) { |
158 | setCurrentDirectory(index); |
159 | } else { |
160 | QModelIndexList selected = m_view->selectionModel()->selectedIndexes(); |
161 | QStringList files; |
162 | foreach (const QModelIndex &i, selected) { |
163 | files << m_model->filePath(i); |
164 | } |
165 | emit fileSelected(files.first()); |
166 | } |
167 | } |
168 | |
169 | void FileSelector::setCurrentDirectory(const QString &dir) |
170 | { |
171 | setCurrentDirectory(m_model->index(dir)); |
172 | } |
173 | |
174 | void FileSelector::setCurrentDirectory(const QModelIndex &dir) |
175 | { |
176 | m_view->setRootIndex(dir); |
177 | QString path = m_model->filePath(dir); |
178 | setPathLabel(path); |
179 | if (m_bookmarks.contains(path)) { |
180 | m_bookmarkButton->setIcon(QIcon(":bookmark.png")); |
181 | } else { |
182 | m_bookmarkButton->setIcon(QIcon(":nobookmark.png")); |
183 | } |
184 | } |
185 | |
186 | void FileSelector::updateBookmarkMenu() |
187 | { |
188 | m_bookmarkMenu->clear(); |
189 | |
190 | if (m_bookmarks.isEmpty()) { |
191 | QAction *action = new QAction("No bookmarks", m_bookmarkMenu); |
192 | action->setIcon(QIcon(":nobookmark.png")); |
193 | m_bookmarkMenu->addAction(action); |
194 | return; |
195 | } |
196 | |
197 | foreach (const QString &bm, m_bookmarks) { |
198 | QAction *action = new QAction(shortText(bm, 290), m_bookmarkMenu); |
199 | action->setIcon(QIcon(":bookmark.png")); |
200 | m_bookmarkMenu->addAction(action); |
201 | connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map())); |
202 | m_signalMapper->setMapping(action, bm); |
203 | } |
204 | } |
205 | |
206 | void FileSelector::setPathLabel(const QString &dir) |
207 | { |
208 | m_path->setText(shortText(dir, 310)); |
209 | } |
210 | |
211 | QString FileSelector::shortText(const QString &text, int width) |
212 | { |
213 | QFontMetrics fm(m_path->font()); |
214 | |
215 | QString shortText = text; |
216 | int n = 0; |
217 | while (fm.width(shortText) > width) { |
218 | ++n; |
219 | shortText = text; |
220 | shortText.replace(0, n, "..."); |
221 | } |
222 | |
223 | return shortText; |
224 | } |
225 | |
226 |
Branches:
master