Changeset View
Changeset View
Standalone View
Standalone View
kate/katequickopen.cpp
Show All 12 Lines | 1 | /* | |||
---|---|---|---|---|---|
13 | along with this library; see the file COPYING.LIB. If not, write to | 13 | along with this library; see the file COPYING.LIB. If not, write to | ||
14 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 14 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
15 | Boston, MA 02110-1301, USA. | 15 | Boston, MA 02110-1301, USA. | ||
16 | --- | 16 | --- | ||
17 | Copyright (C) 2007,2009 Joseph Wenninger <jowenn@kde.org> | 17 | Copyright (C) 2007,2009 Joseph Wenninger <jowenn@kde.org> | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "katequickopen.h" | 20 | #include "katequickopen.h" | ||
21 | #include "katequickopenmodel.h" | ||||
21 | 22 | | |||
22 | #include "katemainwindow.h" | 23 | #include "katemainwindow.h" | ||
23 | #include "kateviewmanager.h" | 24 | #include "kateviewmanager.h" | ||
24 | #include "kateapp.h" | 25 | #include "kateapp.h" | ||
25 | 26 | | |||
26 | #include <ktexteditor/document.h> | 27 | #include <ktexteditor/document.h> | ||
27 | #include <ktexteditor/view.h> | 28 | #include <ktexteditor/view.h> | ||
28 | 29 | | |||
30 | #include <tuple> | ||||
31 | | ||||
29 | #include <KPluginFactory> | 32 | #include <KPluginFactory> | ||
30 | #include <KPluginLoader> | 33 | #include <KPluginLoader> | ||
31 | #include <KAboutData> | 34 | #include <KAboutData> | ||
32 | #include <KLineEdit> | 35 | #include <KLineEdit> | ||
33 | #include <KActionCollection> | 36 | #include <KActionCollection> | ||
34 | #include <KLocalizedString> | 37 | #include <KLocalizedString> | ||
35 | 38 | | |||
36 | #include <QEvent> | 39 | #include <QEvent> | ||
37 | #include <QFileInfo> | 40 | #include <QFileInfo> | ||
38 | #include <QSortFilterProxyModel> | 41 | #include <QSortFilterProxyModel> | ||
39 | #include <QCoreApplication> | 42 | #include <QCoreApplication> | ||
40 | #include <QPointer> | 43 | #include <QPointer> | ||
41 | #include <QStandardItemModel> | 44 | #include <QStandardItemModel> | ||
42 | #include <QDesktopWidget> | 45 | #include <QDesktopWidget> | ||
43 | #include <QBoxLayout> | 46 | #include <QBoxLayout> | ||
44 | #include <QLabel> | 47 | #include <QLabel> | ||
45 | #include <QTreeView> | 48 | #include <QTreeView> | ||
46 | 49 | | |||
47 | Q_DECLARE_METATYPE(QPointer<KTextEditor::Document>) | 50 | Q_DECLARE_METATYPE(QPointer<KTextEditor::Document>) | ||
48 | 51 | | |||
49 | static const int DocumentRole = Qt::UserRole + 1; | | |||
50 | static const int UrlRole = Qt::UserRole + 2; | | |||
51 | static const int SortFilterRole = Qt::UserRole + 3; | | |||
52 | | ||||
53 | KateQuickOpen::KateQuickOpen(QWidget *parent, KateMainWindow *mainWindow) | 52 | KateQuickOpen::KateQuickOpen(QWidget *parent, KateMainWindow *mainWindow) | ||
54 | : QWidget(parent) | 53 | : QWidget(parent) | ||
55 | , m_mainWindow(mainWindow) | 54 | , m_mainWindow(mainWindow) | ||
56 | { | 55 | { | ||
57 | QVBoxLayout *layout = new QVBoxLayout(); | 56 | QVBoxLayout *layout = new QVBoxLayout(); | ||
58 | layout->setSpacing(0); | 57 | layout->setSpacing(0); | ||
59 | layout->setMargin(0); | 58 | layout->setMargin(0); | ||
60 | setLayout(layout); | 59 | setLayout(layout); | ||
61 | 60 | | |||
62 | m_inputLine = new KLineEdit(); | 61 | m_inputLine = new KLineEdit(); | ||
63 | setFocusProxy(m_inputLine); | 62 | setFocusProxy(m_inputLine); | ||
64 | m_inputLine->setPlaceholderText(i18n("Quick Open Search")); | 63 | m_inputLine->setPlaceholderText(i18n("Quick Open Search")); | ||
65 | 64 | | |||
66 | layout->addWidget(m_inputLine); | 65 | layout->addWidget(m_inputLine); | ||
67 | 66 | | |||
68 | m_listView = new QTreeView(); | 67 | m_listView = new QTreeView(); | ||
69 | layout->addWidget(m_listView, 1); | 68 | layout->addWidget(m_listView, 1); | ||
70 | m_listView->setTextElideMode(Qt::ElideLeft); | 69 | m_listView->setTextElideMode(Qt::ElideLeft); | ||
71 | 70 | | |||
72 | m_base_model = new QStandardItemModel(0, 2, this); | 71 | m_base_model = new KateQuickOpenModel(m_mainWindow, this); | ||
73 | 72 | | |||
74 | m_model = new QSortFilterProxyModel(this); | 73 | m_model = new QSortFilterProxyModel(this); | ||
75 | m_model->setFilterRole(SortFilterRole); | 74 | m_model->setFilterRole(Qt::DisplayRole); | ||
76 | m_model->setSortRole(SortFilterRole); | 75 | m_model->setSortRole(Qt::DisplayRole); | ||
77 | m_model->setFilterCaseSensitivity(Qt::CaseInsensitive); | 76 | m_model->setFilterCaseSensitivity(Qt::CaseInsensitive); | ||
78 | m_model->setSortCaseSensitivity(Qt::CaseInsensitive); | 77 | m_model->setSortCaseSensitivity(Qt::CaseInsensitive); | ||
79 | 78 | | |||
80 | connect(m_inputLine, &KLineEdit::textChanged, m_model, &QSortFilterProxyModel::setFilterWildcard); | 79 | connect(m_inputLine, &KLineEdit::textChanged, m_model, &QSortFilterProxyModel::setFilterWildcard); | ||
81 | connect(m_inputLine, &KLineEdit::returnPressed, this, &KateQuickOpen::slotReturnPressed); | 80 | connect(m_inputLine, &KLineEdit::returnPressed, this, &KateQuickOpen::slotReturnPressed); | ||
82 | connect(m_model, &QSortFilterProxyModel::rowsInserted, this, &KateQuickOpen::reselectFirst); | 81 | connect(m_model, &QSortFilterProxyModel::rowsInserted, this, &KateQuickOpen::reselectFirst); | ||
83 | connect(m_model, &QSortFilterProxyModel::rowsRemoved, this, &KateQuickOpen::reselectFirst); | 82 | connect(m_model, &QSortFilterProxyModel::rowsRemoved, this, &KateQuickOpen::reselectFirst); | ||
84 | 83 | | |||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Line(s) | |||||
139 | void KateQuickOpen::reselectFirst() | 138 | void KateQuickOpen::reselectFirst() | ||
140 | { | 139 | { | ||
141 | QModelIndex index = m_model->index(0, 0); | 140 | QModelIndex index = m_model->index(0, 0); | ||
142 | m_listView->setCurrentIndex(index); | 141 | m_listView->setCurrentIndex(index); | ||
143 | } | 142 | } | ||
144 | 143 | | |||
145 | void KateQuickOpen::update() | 144 | void KateQuickOpen::update() | ||
146 | { | 145 | { | ||
147 | /** | 146 | m_base_model->refresh(); | ||
148 | * new base mode creation | | |||
149 | * remove from proxy model before populating to avoid wasting time | | |||
150 | * with repeatedly sorting it | | |||
151 | */ | | |||
152 | m_base_model->clear(); | | |||
153 | m_model->setSourceModel(nullptr); | | |||
154 | | ||||
155 | /** | | |||
156 | * remember local file names to avoid dupes with project files | | |||
157 | */ | | |||
158 | QSet<QString> alreadySeenFiles; | | |||
159 | QSet<KTextEditor::Document *> alreadySeenDocs; | | |||
160 | | ||||
161 | /** | | |||
162 | * get views in lru order | | |||
163 | */ | | |||
164 | const QList<KTextEditor::View *> sortedViews(m_mainWindow->viewManager()->sortedViews()); | | |||
165 | | ||||
166 | /** | | |||
167 | * now insert them in order | | |||
168 | */ | | |||
169 | QModelIndex idxToSelect; | | |||
170 | int linecount = 0; | | |||
171 | foreach (KTextEditor::View *view, sortedViews) { | | |||
172 | KTextEditor::Document *doc = view->document(); | | |||
173 | | ||||
174 | if (alreadySeenDocs.contains(doc)) { | | |||
175 | continue; | | |||
176 | } | | |||
177 | | ||||
178 | alreadySeenDocs.insert(doc); | | |||
179 | | ||||
180 | //QStandardItem *item=new QStandardItem(i18n("%1: %2",doc->documentName(),doc->url().toString())); | | |||
181 | QStandardItem *itemName = new QStandardItem(doc->documentName()); | | |||
182 | | ||||
183 | itemName->setData(qVariantFromValue(QPointer<KTextEditor::Document> (doc)), DocumentRole); | | |||
184 | itemName->setData(QString::fromLatin1("%1: %2").arg(doc->documentName()).arg(doc->url().toString()), SortFilterRole); | | |||
185 | itemName->setEditable(false); | | |||
186 | QFont font = itemName->font(); | | |||
187 | font.setBold(true); | | |||
188 | itemName->setFont(font); | | |||
189 | | ||||
190 | QStandardItem *itemUrl = new QStandardItem(doc->url().toString()); | | |||
191 | itemUrl->setEditable(false); | | |||
192 | m_base_model->setItem(linecount, 0, itemName); | | |||
193 | m_base_model->setItem(linecount, 1, itemUrl); | | |||
194 | linecount++; | | |||
195 | | ||||
196 | if (!doc->url().isEmpty() && doc->url().isLocalFile()) { | | |||
197 | alreadySeenFiles.insert(doc->url().toLocalFile()); | | |||
198 | } | | |||
199 | | ||||
200 | // select second document, that is the last used (beside the active one) | | |||
201 | if (linecount == 2) { | | |||
202 | idxToSelect = itemName->index(); | | |||
203 | } | | |||
204 | } | | |||
205 | | ||||
206 | /** | | |||
207 | * get all open documents | | |||
208 | */ | | |||
209 | QList<KTextEditor::Document *> docs = KateApp::self()->documentManager()->documentList(); | | |||
210 | foreach(KTextEditor::Document * doc, docs) { | | |||
211 | /** | | |||
212 | * skip docs already open | | |||
213 | */ | | |||
214 | if (alreadySeenDocs.contains(doc)) { | | |||
215 | continue; | | |||
216 | } | | |||
217 | | ||||
218 | //QStandardItem *item=new QStandardItem(i18n("%1: %2",doc->documentName(),doc->url().toString())); | | |||
219 | QStandardItem *itemName = new QStandardItem(doc->documentName()); | | |||
220 | | ||||
221 | itemName->setData(qVariantFromValue(QPointer<KTextEditor::Document> (doc)), DocumentRole); | | |||
222 | itemName->setData(QString::fromLatin1("%1: %2").arg(doc->documentName()).arg(doc->url().toString()), SortFilterRole); | | |||
223 | itemName->setEditable(false); | | |||
224 | QFont font = itemName->font(); | | |||
225 | font.setBold(true); | | |||
226 | itemName->setFont(font); | | |||
227 | | ||||
228 | QStandardItem *itemUrl = new QStandardItem(doc->url().toString()); | | |||
229 | itemUrl->setEditable(false); | | |||
230 | m_base_model->setItem(linecount, 0, itemName); | | |||
231 | m_base_model->setItem(linecount, 1, itemUrl); | | |||
232 | linecount++; | | |||
233 | | ||||
234 | if (!doc->url().isEmpty() && doc->url().isLocalFile()) { | | |||
235 | alreadySeenFiles.insert(doc->url().toLocalFile()); | | |||
236 | } | | |||
237 | } | | |||
238 | | ||||
239 | /** | | |||
240 | * insert all project files, if any project around | | |||
241 | */ | | |||
242 | if (QObject *projectView = m_mainWindow->pluginView(QStringLiteral("kateprojectplugin"))) { | | |||
243 | QStringList projectFiles = projectView->property("projectFiles").toStringList(); | | |||
244 | foreach(const QString & file, projectFiles) { | | |||
245 | /** | | |||
246 | * skip files already open | | |||
247 | */ | | |||
248 | if (alreadySeenFiles.contains(file)) { | | |||
249 | continue; | | |||
250 | } | | |||
251 | | ||||
252 | QFileInfo fi(file); | | |||
253 | QStandardItem *itemName = new QStandardItem(fi.fileName()); | | |||
254 | | ||||
255 | itemName->setData(qVariantFromValue(QUrl::fromLocalFile(file)), UrlRole); | | |||
256 | itemName->setData(QString::fromLatin1("%1: %2").arg(fi.fileName()).arg(file), SortFilterRole); | | |||
257 | itemName->setEditable(false); | | |||
258 | | ||||
259 | QStandardItem *itemUrl = new QStandardItem(file); | | |||
260 | itemUrl->setEditable(false); | | |||
261 | m_base_model->setItem(linecount, 0, itemName); | | |||
262 | m_base_model->setItem(linecount, 1, itemUrl); | | |||
263 | linecount++; | | |||
264 | } | | |||
265 | } | | |||
266 | | ||||
267 | if (idxToSelect.isValid()) { | | |||
268 | m_listView->setCurrentIndex(m_model->mapFromSource(idxToSelect)); | | |||
269 | } else { | | |||
270 | reselectFirst(); | | |||
271 | } | | |||
272 | | ||||
273 | m_model->setSourceModel(m_base_model); | | |||
274 | | ||||
275 | /** | | |||
276 | * adjust view | | |||
277 | */ | | |||
278 | m_listView->resizeColumnToContents(0); | 147 | m_listView->resizeColumnToContents(0); | ||
279 | } | 148 | } | ||
anthonyfieroni: Why from latin? | |||||
tcanabrava: this is from the old code and I didn't changed. | |||||
280 | 149 | | |||
281 | void KateQuickOpen::slotReturnPressed() | 150 | void KateQuickOpen::slotReturnPressed() | ||
282 | { | 151 | { | ||
283 | /** | 152 | const auto index = m_listView->model()->index(m_listView->currentIndex().row(), KateQuickOpenModel::Columns::FilePath); | ||
284 | * open document for first element, if possible | 153 | auto url = QUrl(index.data(Qt::DisplayRole).toString()); | ||
285 | * prefer to use the document pointer | | |||
286 | */ | | |||
287 | // our data is in column 0 (clicking on column 1 results in no data, therefore, create new index) | | |||
288 | const QModelIndex index = m_listView->model()->index(m_listView->currentIndex().row(), 0); | | |||
289 | KTextEditor::Document *doc = index.data(DocumentRole).value<QPointer<KTextEditor::Document> >(); | | |||
290 | if (doc) { | | |||
291 | m_mainWindow->wrapper()->activateView(doc); | | |||
292 | } else { | | |||
293 | QUrl url = index.data(UrlRole).value<QUrl>(); | | |||
294 | if (!url.isEmpty()) { | | |||
295 | m_mainWindow->wrapper()->openUrl(url); | 154 | m_mainWindow->wrapper()->openUrl(url); | ||
296 | } | | |||
297 | } | | |||
298 | | ||||
299 | /** | | |||
300 | * in any case, switch back to view manager | | |||
301 | */ | | |||
302 | m_mainWindow->slotWindowActivated(); | 155 | m_mainWindow->slotWindowActivated(); | ||
303 | m_inputLine->clear(); | 156 | m_inputLine->clear(); | ||
304 | } | 157 | } |
Why from latin?