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>
41 ViewExtensionsFactory::ViewExtensionsFactory(QAbstractItemView
* view
,
42 DolphinViewController
* dolphinViewController
,
43 const ViewModeController
* viewModeController
) :
46 m_dolphinViewController(dolphinViewController
),
48 m_previewGenerator(0),
49 m_selectionManager(0),
51 m_fileItemDelegate(0),
52 m_versionControlObserver(0)
54 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
56 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
58 // initialize tooltips
59 if (settings
->showToolTips()) {
60 DolphinSortFilterProxyModel
* proxyModel
= static_cast<DolphinSortFilterProxyModel
*>(view
->model());
61 m_toolTipManager
= new ToolTipManager(view
, proxyModel
);
63 connect(dolphinViewController
, SIGNAL(hideToolTip()),
64 m_toolTipManager
, SLOT(hideTip()));
67 // initialize preview generator
68 Q_ASSERT(view
->iconSize().isValid());
69 m_previewGenerator
= new KFilePreviewGenerator(view
);
70 m_previewGenerator
->setPreviewShown(dolphinViewController
->view()->showPreview());
71 connect(viewModeController
, SIGNAL(zoomLevelChanged(int)),
72 this, SLOT(slotZoomLevelChanged()));
73 connect(viewModeController
, SIGNAL(cancelPreviews()),
74 this, SLOT(cancelPreviews()));
75 connect(dolphinViewController
->view(), SIGNAL(showPreviewChanged()),
76 this, SLOT(slotShowPreviewChanged()));
78 // initialize selection manager
79 if (settings
->showSelectionToggle()) {
80 m_selectionManager
= new SelectionManager(view
);
81 connect(m_selectionManager
, SIGNAL(selectionChanged()),
82 this, SLOT(requestActivation()));
83 connect(viewModeController
, SIGNAL(urlChanged(const KUrl
&)),
84 m_selectionManager
, SLOT(reset()));
87 // initialize auto scroller
88 m_autoScroller
= new DolphinViewAutoScroller(view
);
90 // initialize file item delegate
91 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
92 m_fileItemDelegate
->setShowToolTipWhenElided(false);
93 view
->setItemDelegate(m_fileItemDelegate
);
95 // initialize version control observer
96 const DolphinView
* dolphinView
= dolphinViewController
->view();
97 m_versionControlObserver
= new VersionControlObserver(view
);
98 connect(m_versionControlObserver
, SIGNAL(infoMessage(const QString
&)),
99 dolphinView
, SIGNAL(infoMessage(const QString
&)));
100 connect(m_versionControlObserver
, SIGNAL(errorMessage(const QString
&)),
101 dolphinView
, SIGNAL(errorMessage(const QString
&)));
102 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(const QString
&)),
103 dolphinView
, SIGNAL(operationCompletedMessage(const QString
&)));
104 connect(dolphinViewController
, SIGNAL(requestVersionControlActions(const KFileItemList
&)),
105 this, SLOT(slotRequestVersionControlActions(const KFileItemList
&)));
107 // react on view property changes
108 connect(dolphinView
, SIGNAL(showHiddenFilesChanged()),
109 this, SLOT(slotShowHiddenFilesChanged()));
110 connect(dolphinView
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
111 this, SLOT(slotSortingChanged(DolphinView::Sorting
)));
112 connect(dolphinView
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
113 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
114 connect(dolphinView
, SIGNAL(sortFoldersFirstChanged(bool)),
115 this, SLOT(slotSortFoldersFirstChanged(bool)));
117 // Give the view the ability to auto-expand its directories on hovering
118 // (the column view takes care about this itself). If the details view
119 // uses expandable folders, the auto-expanding should be used always.
120 m_folderExpander
= new FolderExpander(view
, proxyModel());
121 m_folderExpander
->setEnabled(settings
->autoExpandFolders());
122 connect(m_folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
123 dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
125 // react on namefilter changes
126 connect(viewModeController
, SIGNAL(nameFilterChanged(const QString
&)),
127 this, SLOT(slotNameFilterChanged(const QString
&)));
129 view
->viewport()->installEventFilter(this);
132 ViewExtensionsFactory::~ViewExtensionsFactory()
136 void ViewExtensionsFactory::handleCurrentIndexChange(const QModelIndex
& current
, const QModelIndex
& previous
)
138 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
141 DolphinFileItemDelegate
* ViewExtensionsFactory::fileItemDelegate() const
143 return m_fileItemDelegate
;
146 void ViewExtensionsFactory::setAutoFolderExpandingEnabled(bool enabled
)
148 m_folderExpander
->setEnabled(enabled
);
151 bool ViewExtensionsFactory::autoFolderExpandingEnabled() const
153 return m_folderExpander
->enabled();
156 bool ViewExtensionsFactory::eventFilter(QObject
* watched
, QEvent
* event
)
159 if ((event
->type() == QEvent::Wheel
) && (m_selectionManager
!= 0)) {
160 m_selectionManager
->reset();
165 void ViewExtensionsFactory::slotZoomLevelChanged()
167 m_previewGenerator
->updateIcons();
168 if (m_selectionManager
!= 0) {
169 m_selectionManager
->reset();
173 void ViewExtensionsFactory::cancelPreviews()
175 m_previewGenerator
->cancelPreviews();
178 void ViewExtensionsFactory::slotShowPreviewChanged()
180 const bool show
= m_dolphinViewController
->view()->showPreview();
181 m_previewGenerator
->setPreviewShown(show
);
184 void ViewExtensionsFactory::slotShowHiddenFilesChanged()
186 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel()->sourceModel());
187 KDirLister
* dirLister
= dirModel
->dirLister();
191 const bool show
= m_dolphinViewController
->view()->showHiddenFiles();
192 dirLister
->setShowingDotFiles(show
);
194 const KUrl url
= dirLister
->url();
196 dirLister
->openUrl(url
, KDirLister::NoFlags
);
200 void ViewExtensionsFactory::slotSortingChanged(DolphinView::Sorting sorting
)
202 proxyModel()->setSorting(sorting
);
205 void ViewExtensionsFactory::slotSortOrderChanged(Qt::SortOrder order
)
207 proxyModel()->setSortOrder(order
);
210 void ViewExtensionsFactory::slotSortFoldersFirstChanged(bool foldersFirst
)
212 proxyModel()->setSortFoldersFirst(foldersFirst
);
215 void ViewExtensionsFactory::slotNameFilterChanged(const QString
& nameFilter
)
217 proxyModel()->setFilterFixedString(nameFilter
);
220 void ViewExtensionsFactory::slotRequestVersionControlActions(const KFileItemList
& items
)
222 QList
<QAction
*> actions
;
223 if (items
.isEmpty()) {
224 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel()->sourceModel());
225 const KUrl url
= dirModel
->dirLister()->url();
226 actions
= m_versionControlObserver
->contextMenuActions(url
.path(KUrl::AddTrailingSlash
));
228 actions
= m_versionControlObserver
->contextMenuActions(items
);
230 m_dolphinViewController
->setVersionControlActions(actions
);
233 void ViewExtensionsFactory::requestActivation()
235 m_dolphinViewController
->requestActivation();
238 DolphinSortFilterProxyModel
* ViewExtensionsFactory::proxyModel() const
240 return static_cast<DolphinSortFilterProxyModel
*>(m_view
->model());
243 #include "viewextensionsfactory.moc"