]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Fix small problems with enums. Sort by type works like a charm :)
[dolphin.git] / src / dolphinviewcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
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 *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "dolphinviewcontainer.h"
21
22 #include <QtGui/QApplication>
23 #include <QtGui/QClipboard>
24 #include <QtGui/QKeyEvent>
25 #include <QtGui/QItemSelection>
26 #include <QtGui/QBoxLayout>
27 #include <QtCore/QTimer>
28 #include <QtGui/QScrollBar>
29
30 #include <kdirmodel.h>
31 #include <kfileitemdelegate.h>
32 #include <kfileplacesmodel.h>
33 #include <kglobalsettings.h>
34 #include <klocale.h>
35 #include <kiconeffect.h>
36 #include <kio/netaccess.h>
37 #include <kio/renamedialog.h>
38 #include <kio/previewjob.h>
39 #include <kmimetyperesolver.h>
40 #include <konqmimedata.h>
41 #include <konq_operations.h>
42 #include <kurl.h>
43
44 #include "dolphincolumnview.h"
45 #include "dolphincontroller.h"
46 #include "dolphinstatusbar.h"
47 #include "dolphinmainwindow.h"
48 #include "dolphindirlister.h"
49 #include "dolphinsortfilterproxymodel.h"
50 #include "dolphindetailsview.h"
51 #include "dolphiniconsview.h"
52 #include "dolphincontextmenu.h"
53 #include "dolphinitemcategorizer.h"
54 #include "filterbar.h"
55 #include "renamedialog.h"
56 #include "kurlnavigator.h"
57 #include "viewproperties.h"
58 #include "dolphinsettings.h"
59 #include "dolphin_generalsettings.h"
60
61 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow* mainWindow,
62 QWidget* parent,
63 const KUrl& url,
64 DolphinView::Mode mode,
65 bool showHiddenFiles) :
66 QWidget(parent),
67 m_showProgress(false),
68 m_folderCount(0),
69 m_fileCount(0),
70 m_mainWindow(mainWindow),
71 m_topLayout(0),
72 m_urlNavigator(0),
73 m_view(0),
74 m_filterBar(0),
75 m_statusBar(0),
76 m_dirModel(0),
77 m_dirLister(0),
78 m_proxyModel(0)
79 {
80 hide();
81 setFocusPolicy(Qt::StrongFocus);
82 m_topLayout = new QVBoxLayout(this);
83 m_topLayout->setSpacing(0);
84 m_topLayout->setMargin(0);
85
86 QClipboard* clipboard = QApplication::clipboard();
87 connect(clipboard, SIGNAL(dataChanged()),
88 this, SLOT(updateCutItems()));
89
90 m_urlNavigator = new KUrlNavigator(DolphinSettings::instance().placesModel(), url, this);
91 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&)),
92 m_mainWindow, SLOT(dropUrls(const KUrl::List&, const KUrl&)));
93 connect(m_urlNavigator, SIGNAL(activated()),
94 this, SLOT(activate()));
95
96 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
97 m_urlNavigator->setUrlEditable(settings->editableUrl());
98 m_urlNavigator->setHomeUrl(settings->homeUrl());
99
100 m_dirLister = new DolphinDirLister();
101 m_dirLister->setAutoUpdate(true);
102 m_dirLister->setMainWindow(this);
103 m_dirLister->setShowingDotFiles(showHiddenFiles);
104 m_dirLister->setDelayedMimeTypes(true);
105
106 m_dirModel = new KDirModel();
107 m_dirModel->setDirLister(m_dirLister);
108 m_dirModel->setDropsAllowed(KDirModel::DropOnDirectory);
109
110 m_proxyModel = new DolphinSortFilterProxyModel(this);
111 m_proxyModel->setSourceModel(m_dirModel);
112
113 connect(m_dirLister, SIGNAL(clear()),
114 this, SLOT(updateStatusBar()));
115 connect(m_dirLister, SIGNAL(percent(int)),
116 this, SLOT(updateProgress(int)));
117 connect(m_dirLister, SIGNAL(deleteItem(KFileItem*)),
118 this, SLOT(updateStatusBar()));
119 connect(m_dirLister, SIGNAL(completed()),
120 this, SLOT(updateItemCount()));
121 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
122 this, SLOT(showInfoMessage(const QString&)));
123 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
124 this, SLOT(showErrorMessage(const QString&)));
125
126 m_view = new DolphinView(this,
127 url,
128 m_dirLister,
129 m_dirModel,
130 m_proxyModel,
131 mode);
132 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
133 m_urlNavigator, SLOT(setUrl(const KUrl&)));
134 connect(m_view, SIGNAL(requestContextMenu(KFileItem*, const KUrl&)),
135 this, SLOT(openContextMenu(KFileItem*, const KUrl&)));
136 connect(m_view, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&)),
137 m_mainWindow, SLOT(dropUrls(const KUrl::List&, const KUrl&)));
138 connect(m_view, SIGNAL(contentsMoved(int, int)),
139 this, SLOT(saveContentsPos(int, int)));
140 connect(m_view, SIGNAL(requestItemInfo(const KUrl&)),
141 this, SLOT(showItemInfo(const KUrl&)));
142 connect(m_view, SIGNAL(errorMessage(const QString&)),
143 this, SLOT(showErrorMessage(const QString&)));
144 connect(m_view, SIGNAL(infoMessage(const QString&)),
145 this, SLOT(showInfoMessage(const QString&)));
146
147 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
148 m_view, SLOT(setUrl(const KUrl&)));
149
150 m_statusBar = new DolphinStatusBar(this, url);
151
152 m_filterBar = new FilterBar(this);
153 m_filterBar->setVisible(settings->filterBar());
154 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
155 this, SLOT(changeNameFilter(const QString&)));
156 connect(m_filterBar, SIGNAL(closeRequest()),
157 this, SLOT(closeFilterBar()));
158
159 m_topLayout->addWidget(m_urlNavigator);
160 m_topLayout->addWidget(m_view);
161 m_topLayout->addWidget(m_filterBar);
162 m_topLayout->addWidget(m_statusBar);
163 }
164
165 DolphinViewContainer::~DolphinViewContainer()
166 {
167 delete m_dirLister;
168 m_dirLister = 0;
169 }
170
171 void DolphinViewContainer::setUrl(const KUrl& url)
172 {
173 m_urlNavigator->setUrl(url);
174 }
175
176 const KUrl& DolphinViewContainer::url() const
177 {
178 return m_urlNavigator->url();
179 }
180
181 void DolphinViewContainer::setActive(bool active)
182 {
183 m_urlNavigator->setActive(active);
184 m_view->setActive(active);
185 }
186
187 bool DolphinViewContainer::isActive() const
188 {
189 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
190 return m_view->isActive();
191 }
192
193 void DolphinViewContainer::renameSelectedItems()
194 {
195 DolphinViewContainer* view = m_mainWindow->activeViewContainer();
196 const KUrl::List urls = m_view->selectedUrls();
197 if (urls.count() > 1) {
198 // More than one item has been selected for renaming. Open
199 // a rename dialog and rename all items afterwards.
200 RenameDialog dialog(urls);
201 if (dialog.exec() == QDialog::Rejected) {
202 return;
203 }
204
205 const QString& newName = dialog.newName();
206 if (newName.isEmpty()) {
207 view->statusBar()->setMessage(dialog.errorString(),
208 DolphinStatusBar::Error);
209 } else {
210 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
211 // as one operation instead of n rename operations like it is done now...
212 Q_ASSERT(newName.contains('#'));
213
214 // iterate through all selected items and rename them...
215 const int replaceIndex = newName.indexOf('#');
216 Q_ASSERT(replaceIndex >= 0);
217 int index = 1;
218
219 KUrl::List::const_iterator it = urls.begin();
220 KUrl::List::const_iterator end = urls.end();
221 while (it != end) {
222 const KUrl& oldUrl = *it;
223 QString number;
224 number.setNum(index++);
225
226 QString name(newName);
227 name.replace(replaceIndex, 1, number);
228
229 if (oldUrl.fileName() != name) {
230 KUrl newUrl = oldUrl;
231 newUrl.setFileName(name);
232 m_mainWindow->rename(oldUrl, newUrl);
233 }
234 ++it;
235 }
236 }
237 } else {
238 // Only one item has been selected for renaming. Use the custom
239 // renaming mechanism from the views.
240 Q_ASSERT(urls.count() == 1);
241
242 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
243 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
244 // is a benefit for the user at all -> let's wait for some input first...
245 RenameDialog dialog(urls);
246 if (dialog.exec() == QDialog::Rejected) {
247 return;
248 }
249
250 const QString& newName = dialog.newName();
251 if (newName.isEmpty()) {
252 view->statusBar()->setMessage(dialog.errorString(),
253 DolphinStatusBar::Error);
254 } else {
255 const KUrl& oldUrl = urls.first();
256 KUrl newUrl = oldUrl;
257 newUrl.setFileName(newName);
258 m_mainWindow->rename(oldUrl, newUrl);
259 }
260 }
261 }
262
263 bool DolphinViewContainer::isFilterBarVisible() const
264 {
265 return m_filterBar->isVisible();
266 }
267
268 bool DolphinViewContainer::isUrlEditable() const
269 {
270 return m_urlNavigator->isUrlEditable();
271 }
272
273 KFileItem* DolphinViewContainer::fileItem(const QModelIndex index) const
274 {
275 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
276 return m_dirModel->itemForIndex(dirModelIndex);
277 }
278
279 void DolphinViewContainer::updateProgress(int percent)
280 {
281 if (!m_showProgress) {
282 // Only show the directory loading progress if the status bar does
283 // not contain another progress information. This means that
284 // the directory loading progress information has the lowest priority.
285 const QString progressText(m_statusBar->progressText());
286 m_showProgress = progressText.isEmpty() ||
287 (progressText == i18n("Loading folder..."));
288 if (m_showProgress) {
289 m_statusBar->setProgressText(i18n("Loading folder..."));
290 m_statusBar->setProgress(0);
291 }
292 }
293
294 if (m_showProgress) {
295 m_statusBar->setProgress(percent);
296 }
297 }
298
299 void DolphinViewContainer::updateItemCount()
300 {
301 if (m_showProgress) {
302 m_statusBar->setProgressText(QString());
303 m_statusBar->setProgress(100);
304 m_showProgress = false;
305 }
306
307 KFileItemList items(m_dirLister->items());
308 KFileItemList::const_iterator it = items.begin();
309 const KFileItemList::const_iterator end = items.end();
310
311 m_fileCount = 0;
312 m_folderCount = 0;
313
314 while (it != end) {
315 KFileItem* item = *it;
316 if (item->isDir()) {
317 ++m_folderCount;
318 } else {
319 ++m_fileCount;
320 }
321 ++it;
322 }
323
324 updateStatusBar();
325
326 QTimer::singleShot(100, this, SLOT(restoreContentsPos()));
327 }
328
329 void DolphinViewContainer::showItemInfo(const KUrl& url)
330 {
331 if (url.isEmpty()) {
332 m_statusBar->clear();
333 return;
334 }
335
336 const QModelIndex index = m_dirModel->indexForUrl(url);
337 const KFileItem* item = m_dirModel->itemForIndex(index);
338 if (item != 0) {
339 m_statusBar->setMessage(item->getStatusBarInfo(), DolphinStatusBar::Default);
340 }
341 }
342
343 void DolphinViewContainer::showInfoMessage(const QString& msg)
344 {
345 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
346 }
347
348 void DolphinViewContainer::showErrorMessage(const QString& msg)
349 {
350 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
351 }
352
353 void DolphinViewContainer::closeFilterBar()
354 {
355 m_filterBar->hide();
356 emit showFilterBarChanged(false);
357 }
358
359 QString DolphinViewContainer::defaultStatusBarText() const
360 {
361 return KIO::itemsSummaryString(m_fileCount + m_folderCount,
362 m_fileCount,
363 m_folderCount,
364 0, false);
365 }
366
367 QString DolphinViewContainer::selectionStatusBarText() const
368 {
369 QString text;
370 const KFileItemList list = m_view->selectedItems();
371 if (list.isEmpty()) {
372 // when an item is triggered, it is temporary selected but selectedItems()
373 // will return an empty list
374 return QString();
375 }
376
377 int fileCount = 0;
378 int folderCount = 0;
379 KIO::filesize_t byteSize = 0;
380 KFileItemList::const_iterator it = list.begin();
381 const KFileItemList::const_iterator end = list.end();
382 while (it != end) {
383 KFileItem* item = *it;
384 if (item->isDir()) {
385 ++folderCount;
386 } else {
387 ++fileCount;
388 byteSize += item->size();
389 }
390 ++it;
391 }
392
393 if (folderCount > 0) {
394 text = i18np("1 Folder selected", "%1 Folders selected", folderCount);
395 if (fileCount > 0) {
396 text += ", ";
397 }
398 }
399
400 if (fileCount > 0) {
401 const QString sizeText(KIO::convertSize(byteSize));
402 text += i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount, sizeText);
403 }
404
405 return text;
406 }
407
408 void DolphinViewContainer::showFilterBar(bool show)
409 {
410 Q_ASSERT(m_filterBar != 0);
411 m_filterBar->setVisible(show);
412 }
413
414 void DolphinViewContainer::updateStatusBar()
415 {
416 // As the item count information is less important
417 // in comparison with other messages, it should only
418 // be shown if:
419 // - the status bar is empty or
420 // - shows already the item count information or
421 // - shows only a not very important information
422 // - if any progress is given don't show the item count info at all
423 const QString msg(m_statusBar->message());
424 const bool updateStatusBarMsg = (msg.isEmpty() ||
425 (msg == m_statusBar->defaultText()) ||
426 (m_statusBar->type() == DolphinStatusBar::Information)) &&
427 (m_statusBar->progress() == 100);
428
429 const QString text(m_view->hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
430 m_statusBar->setDefaultText(text);
431
432 if (updateStatusBarMsg) {
433 m_statusBar->setMessage(text, DolphinStatusBar::Default);
434 }
435 }
436
437 void DolphinViewContainer::changeNameFilter(const QString& nameFilter)
438 {
439 // The name filter of KDirLister does a 'hard' filtering, which
440 // means that only the items are shown where the names match
441 // exactly the filter. This is non-transparent for the user, which
442 // just wants to have a 'soft' filtering: does the name contain
443 // the filter string?
444 QString adjustedFilter(nameFilter);
445 adjustedFilter.insert(0, '*');
446 adjustedFilter.append('*');
447
448 // Use the ProxyModel to filter:
449 // This code is #ifdefed as setNameFilter behaves
450 // slightly different than the QSortFilterProxyModel
451 // as it will not remove directories. I will ask
452 // our beloved usability experts for input
453 // -- z.
454 #if 0
455 m_dirLister->setNameFilter(adjustedFilter);
456 m_dirLister->emitChanges();
457 #else
458 m_proxyModel->setFilterRegExp(nameFilter);
459 #endif
460 }
461
462 void DolphinViewContainer::openContextMenu(KFileItem* item,
463 const KUrl& url)
464 {
465 DolphinContextMenu contextMenu(m_mainWindow, item, url);
466 contextMenu.open();
467 }
468
469 void DolphinViewContainer::saveContentsPos(int x, int y)
470 {
471 m_urlNavigator->savePosition(x, y);
472 }
473
474 void DolphinViewContainer::restoreContentsPos()
475 {
476 if (!url().isEmpty()) {
477 const QPoint pos = m_urlNavigator->savedPosition();
478 m_view->setContentsPosition(pos.x(), pos.y());
479 }
480 }
481
482 void DolphinViewContainer::activate()
483 {
484 setActive(true);
485 }
486
487 #include "dolphinviewcontainer.moc"