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"
38 #include <KFilePreviewGenerator>
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 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()));
86 // initialize auto scroller
87 m_autoScroller
= new DolphinViewAutoScroller(view
);
89 // initialize file item delegate
90 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
91 m_fileItemDelegate
->setShowToolTipWhenElided(false);
92 view
->setItemDelegate(m_fileItemDelegate
);
94 // initialize version control observer
95 const DolphinView
* dolphinView
= dolphinViewController
->view();
96 m_versionControlObserver
= new VersionControlObserver(view
);
97 connect(m_versionControlObserver
, SIGNAL(infoMessage(const QString
&)),
98 dolphinView
, SIGNAL(infoMessage(const QString
&)));
99 connect(m_versionControlObserver
, SIGNAL(errorMessage(const QString
&)),
100 dolphinView
, SIGNAL(errorMessage(const QString
&)));
101 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(const QString
&)),
102 dolphinView
, SIGNAL(operationCompletedMessage(const QString
&)));
103 connect(dolphinViewController
, SIGNAL(requestVersionControlActions(const KFileItemList
&)),
104 this, SLOT(slotRequestVersionControlActions(const KFileItemList
&)));
106 // react on view property changes
107 connect(dolphinView
, SIGNAL(showHiddenFilesChanged()),
108 this, SLOT(slotShowHiddenFilesChanged()));
109 connect(dolphinView
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
110 this, SLOT(slotSortingChanged(DolphinView::Sorting
)));
111 connect(dolphinView
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
112 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
113 connect(dolphinView
, SIGNAL(sortFoldersFirstChanged(bool)),
114 this, SLOT(slotSortFoldersFirstChanged(bool)));
116 // Give the view the ability to auto-expand its directories on hovering
117 // (the column view takes care about this itself). If the details view
118 // uses expandable folders, the auto-expanding should be used always.
119 m_folderExpander
= new FolderExpander(view
, proxyModel());
120 m_folderExpander
->setEnabled(settings
->autoExpandFolders());
121 connect(m_folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
122 dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
124 // react on namefilter changes
125 connect(viewModeController
, SIGNAL(nameFilterChanged(const QString
&)),
126 this, SLOT(slotNameFilterChanged(const QString
&)));
128 view
->viewport()->installEventFilter(this);
131 ViewExtensionsFactory::~ViewExtensionsFactory()
135 void ViewExtensionsFactory::handleCurrentIndexChange(const QModelIndex
& current
, const QModelIndex
& previous
)
137 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
140 DolphinFileItemDelegate
* ViewExtensionsFactory::fileItemDelegate() const
142 return m_fileItemDelegate
;
145 void ViewExtensionsFactory::setAutoFolderExpandingEnabled(bool enabled
)
147 m_folderExpander
->setEnabled(enabled
);
150 bool ViewExtensionsFactory::autoFolderExpandingEnabled() const
152 return m_folderExpander
->enabled();
155 bool ViewExtensionsFactory::eventFilter(QObject
* watched
, QEvent
* event
)
158 if ((event
->type() == QEvent::Wheel
) && (m_selectionManager
!= 0)) {
159 m_selectionManager
->reset();
164 void ViewExtensionsFactory::slotZoomLevelChanged()
166 m_previewGenerator
->updateIcons();
167 if (m_selectionManager
!= 0) {
168 m_selectionManager
->reset();
172 void ViewExtensionsFactory::cancelPreviews()
174 m_previewGenerator
->cancelPreviews();
177 void ViewExtensionsFactory::slotShowPreviewChanged()
179 const bool show
= m_dolphinViewController
->view()->showPreview();
180 m_previewGenerator
->setPreviewShown(show
);
183 void ViewExtensionsFactory::slotShowHiddenFilesChanged()
185 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel()->sourceModel());
186 KDirLister
* dirLister
= dirModel
->dirLister();
190 const bool show
= m_dolphinViewController
->view()->showHiddenFiles();
191 dirLister
->setShowingDotFiles(show
);
193 const KUrl url
= dirLister
->url();
195 dirLister
->openUrl(url
, KDirLister::NoFlags
);
199 void ViewExtensionsFactory::slotSortingChanged(DolphinView::Sorting sorting
)
201 proxyModel()->setSorting(sorting
);
204 void ViewExtensionsFactory::slotSortOrderChanged(Qt::SortOrder order
)
206 proxyModel()->setSortOrder(order
);
209 void ViewExtensionsFactory::slotSortFoldersFirstChanged(bool foldersFirst
)
211 proxyModel()->setSortFoldersFirst(foldersFirst
);
214 void ViewExtensionsFactory::slotNameFilterChanged(const QString
& nameFilter
)
216 proxyModel()->setFilterFixedString(nameFilter
);
219 void ViewExtensionsFactory::slotRequestVersionControlActions(const KFileItemList
& items
)
221 QList
<QAction
*> actions
;
222 if (items
.isEmpty()) {
223 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel()->sourceModel());
224 const KUrl url
= dirModel
->dirLister()->url();
225 actions
= m_versionControlObserver
->contextMenuActions(url
.path(KUrl::AddTrailingSlash
));
227 actions
= m_versionControlObserver
->contextMenuActions(items
);
229 m_dolphinViewController
->setVersionControlActions(actions
);
232 void ViewExtensionsFactory::requestActivation()
234 m_dolphinViewController
->requestActivation();
237 DolphinSortFilterProxyModel
* ViewExtensionsFactory::proxyModel() const
239 return static_cast<DolphinSortFilterProxyModel
*>(m_view
->model());
242 #include "viewextensionsfactory.moc"