1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "viewextensionsfactory.h"
22 #include "dolphinfileitemdelegate.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinviewcontroller.h"
26 #include "dolphinviewautoscroller.h"
27 #include "folderexpander.h"
28 #include "selectionmanager.h"
29 #include "settings/dolphinsettings.h"
30 #include "tooltips/tooltipmanager.h"
31 #include "versioncontrol/versioncontrolobserver.h"
32 #include "viewmodecontroller.h"
34 #include "dolphin_generalsettings.h"
36 #include <kdirlister.h>
37 #include <kdirmodel.h>
38 #include <kfilepreviewgenerator.h>
39 #include <QAbstractItemView>
40 #include <QApplication>
42 ViewExtensionsFactory::ViewExtensionsFactory(QAbstractItemView
* view
,
43 DolphinViewController
* dolphinViewController
,
44 const ViewModeController
* viewModeController
) :
47 m_dolphinViewController(dolphinViewController
),
49 m_previewGenerator(0),
50 m_selectionManager(0),
52 m_fileItemDelegate(0),
53 m_versionControlObserver(0)
55 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
57 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
59 // initialize tooltips
60 if (settings
->showToolTips()) {
61 DolphinSortFilterProxyModel
* proxyModel
= static_cast<DolphinSortFilterProxyModel
*>(view
->model());
62 m_toolTipManager
= new ToolTipManager(view
, proxyModel
);
64 connect(dolphinViewController
, SIGNAL(hideToolTip()),
65 m_toolTipManager
, SLOT(hideToolTip()));
68 // initialize preview generator
69 Q_ASSERT(view
->iconSize().isValid());
70 m_previewGenerator
= new KFilePreviewGenerator(view
);
71 m_previewGenerator
->setPreviewShown(dolphinViewController
->view()->showPreview());
72 connect(viewModeController
, SIGNAL(zoomLevelChanged(int)),
73 this, SLOT(slotZoomLevelChanged()));
74 connect(viewModeController
, SIGNAL(cancelPreviews()),
75 this, SLOT(cancelPreviews()));
76 connect(dolphinViewController
->view(), SIGNAL(showPreviewChanged()),
77 this, SLOT(slotShowPreviewChanged()));
79 // initialize selection manager
80 if (settings
->showSelectionToggle()) {
81 m_selectionManager
= new SelectionManager(view
);
82 connect(m_selectionManager
, SIGNAL(selectionChanged()),
83 this, SLOT(requestActivation()));
84 connect(viewModeController
, SIGNAL(urlChanged(const KUrl
&)),
85 m_selectionManager
, SLOT(reset()));
88 // initialize auto scroller
89 m_autoScroller
= new DolphinViewAutoScroller(view
);
91 // initialize file item delegate
92 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
93 m_fileItemDelegate
->setShowToolTipWhenElided(false);
94 view
->setItemDelegate(m_fileItemDelegate
);
96 // initialize version control observer
97 const DolphinView
* dolphinView
= dolphinViewController
->view();
98 m_versionControlObserver
= new VersionControlObserver(view
);
99 connect(m_versionControlObserver
, SIGNAL(infoMessage(const QString
&)),
100 dolphinView
, SIGNAL(infoMessage(const QString
&)));
101 connect(m_versionControlObserver
, SIGNAL(errorMessage(const QString
&)),
102 dolphinView
, SIGNAL(errorMessage(const QString
&)));
103 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(const QString
&)),
104 dolphinView
, SIGNAL(operationCompletedMessage(const QString
&)));
105 connect(dolphinViewController
, SIGNAL(requestVersionControlActions(const KFileItemList
&)),
106 this, SLOT(slotRequestVersionControlActions(const KFileItemList
&)));
108 // react on view property changes
109 connect(dolphinView
, SIGNAL(showHiddenFilesChanged()),
110 this, SLOT(slotShowHiddenFilesChanged()));
111 connect(dolphinView
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
112 this, SLOT(slotSortingChanged(DolphinView::Sorting
)));
113 connect(dolphinView
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
114 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
115 connect(dolphinView
, SIGNAL(sortFoldersFirstChanged(bool)),
116 this, SLOT(slotSortFoldersFirstChanged(bool)));
118 // Give the view the ability to auto-expand its directories on hovering
119 // (the column view takes care about this itself). If the details view
120 // uses expandable folders, the auto-expanding should be used always.
121 m_folderExpander
= new FolderExpander(view
, proxyModel());
122 m_folderExpander
->setEnabled(settings
->autoExpandFolders());
123 connect(m_folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
124 dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
126 // react on namefilter changes
127 connect(viewModeController
, SIGNAL(nameFilterChanged(const QString
&)),
128 this, SLOT(slotNameFilterChanged(const QString
&)));
130 view
->viewport()->installEventFilter(this);
133 ViewExtensionsFactory::~ViewExtensionsFactory()
137 void ViewExtensionsFactory::handleCurrentIndexChange(const QModelIndex
& current
, const QModelIndex
& previous
)
139 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
142 DolphinFileItemDelegate
* ViewExtensionsFactory::fileItemDelegate() const
144 return m_fileItemDelegate
;
147 void ViewExtensionsFactory::setAutoFolderExpandingEnabled(bool enabled
)
149 m_folderExpander
->setEnabled(enabled
);
152 bool ViewExtensionsFactory::autoFolderExpandingEnabled() const
154 return m_folderExpander
->enabled();
157 bool ViewExtensionsFactory::eventFilter(QObject
* watched
, QEvent
* event
)
160 if ((event
->type() == QEvent::Wheel
) && (m_selectionManager
!= 0)) {
161 m_selectionManager
->reset();
166 void ViewExtensionsFactory::slotZoomLevelChanged()
168 m_previewGenerator
->updateIcons();
169 if (m_selectionManager
!= 0) {
170 m_selectionManager
->reset();
174 void ViewExtensionsFactory::cancelPreviews()
176 m_previewGenerator
->cancelPreviews();
179 void ViewExtensionsFactory::slotShowPreviewChanged()
181 const bool show
= m_dolphinViewController
->view()->showPreview();
182 m_previewGenerator
->setPreviewShown(show
);
185 void ViewExtensionsFactory::slotShowHiddenFilesChanged()
187 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel()->sourceModel());
188 KDirLister
* dirLister
= dirModel
->dirLister();
192 const bool show
= m_dolphinViewController
->view()->showHiddenFiles();
193 dirLister
->setShowingDotFiles(show
);
195 const KUrl url
= dirLister
->url();
197 dirLister
->openUrl(url
, KDirLister::NoFlags
);
201 void ViewExtensionsFactory::slotSortingChanged(DolphinView::Sorting sorting
)
203 proxyModel()->setSorting(sorting
);
206 void ViewExtensionsFactory::slotSortOrderChanged(Qt::SortOrder order
)
208 proxyModel()->setSortOrder(order
);
211 void ViewExtensionsFactory::slotSortFoldersFirstChanged(bool foldersFirst
)
213 proxyModel()->setSortFoldersFirst(foldersFirst
);
216 void ViewExtensionsFactory::slotNameFilterChanged(const QString
& nameFilter
)
218 proxyModel()->setFilterFixedString(nameFilter
);
221 void ViewExtensionsFactory::slotRequestVersionControlActions(const KFileItemList
& items
)
223 QList
<QAction
*> actions
;
224 if (items
.isEmpty()) {
225 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel()->sourceModel());
226 const KUrl url
= dirModel
->dirLister()->url();
227 actions
= m_versionControlObserver
->contextMenuActions(url
.path(KUrl::AddTrailingSlash
));
229 actions
= m_versionControlObserver
->contextMenuActions(items
);
231 m_dolphinViewController
->setVersionControlActions(actions
);
234 void ViewExtensionsFactory::requestActivation()
236 m_dolphinViewController
->requestActivation();
239 DolphinSortFilterProxyModel
* ViewExtensionsFactory::proxyModel() const
241 return static_cast<DolphinSortFilterProxyModel
*>(m_view
->model());
244 #include "viewextensionsfactory.moc"