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 "dolphincontroller.h"
23 #include "dolphinfileitemdelegate.h"
24 #include "dolphinsortfilterproxymodel.h"
25 #include "dolphinview.h"
26 #include "dolphinviewautoscroller.h"
27 #include "selectionmanager.h"
28 #include "settings/dolphinsettings.h"
29 #include "tooltips/tooltipmanager.h"
31 #include "dolphin_generalsettings.h"
33 #include <kdirlister.h>
34 #include <kdirmodel.h>
35 #include <kfilepreviewgenerator.h>
36 #include <QAbstractItemView>
38 ViewExtensionsFactory::ViewExtensionsFactory(QAbstractItemView
* view
,
39 DolphinController
* controller
) :
42 m_controller(controller
),
44 m_previewGenerator(0),
45 m_selectionManager(0),
49 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
51 // initialize tooltips
52 if (settings
->showToolTips()) {
53 DolphinSortFilterProxyModel
* proxyModel
= static_cast<DolphinSortFilterProxyModel
*>(view
->model());
54 m_toolTipManager
= new ToolTipManager(view
, proxyModel
);
56 connect(controller
, SIGNAL(hideToolTip()),
57 m_toolTipManager
, SLOT(hideTip()));
60 // initialize preview generator
61 m_previewGenerator
= new KFilePreviewGenerator(view
);
62 m_previewGenerator
->setPreviewShown(controller
->dolphinView()->showPreview());
63 connect(controller
, SIGNAL(zoomLevelChanged(int)),
64 this, SLOT(slotZoomLevelChanged()));
65 connect(controller
, SIGNAL(cancelPreviews()),
66 this, SLOT(cancelPreviews()));
67 connect(controller
->dolphinView(), SIGNAL(showPreviewChanged()),
68 this, SLOT(slotShowPreviewChanged()));
70 // initialize selection manager
71 if (settings
->showSelectionToggle()) {
72 m_selectionManager
= new SelectionManager(view
);
73 connect(m_selectionManager
, SIGNAL(selectionChanged()),
74 this, SLOT(requestActivation()));
75 connect(controller
, SIGNAL(urlChanged(const KUrl
&)),
76 m_selectionManager
, SLOT(reset()));
79 // initialize auto scroller
80 m_autoScroller
= new DolphinViewAutoScroller(view
);
82 // initialize file item delegate
83 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
84 m_fileItemDelegate
->setShowToolTipWhenElided(false);
85 view
->setItemDelegate(m_fileItemDelegate
);
87 // react on view property changes
88 const DolphinView
* dolphinView
= controller
->dolphinView();
89 connect(dolphinView
, SIGNAL(showHiddenFilesChanged()),
90 this, SLOT(slotShowHiddenFilesChanged()));
91 connect(dolphinView
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
92 this, SLOT(slotSortingChanged(DolphinView::Sorting
)));
93 connect(dolphinView
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
94 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
95 connect(dolphinView
, SIGNAL(sortFoldersFirstChanged(bool)),
96 this, SLOT(slotSortFoldersFirstChanged(bool)));
98 connect(controller
, SIGNAL(nameFilterChanged(const QString
&)),
99 this, SLOT(slotNameFilterChanged(const QString
&)));
101 view
->viewport()->installEventFilter(this);
104 ViewExtensionsFactory::~ViewExtensionsFactory()
108 void ViewExtensionsFactory::handleCurrentIndexChange(const QModelIndex
& current
, const QModelIndex
& previous
)
110 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
113 DolphinFileItemDelegate
* ViewExtensionsFactory::fileItemDelegate() const
115 return m_fileItemDelegate
;
118 bool ViewExtensionsFactory::eventFilter(QObject
* watched
, QEvent
* event
)
121 if ((event
->type() == QEvent::Wheel
) && (m_selectionManager
!= 0)) {
122 m_selectionManager
->reset();
127 void ViewExtensionsFactory::slotZoomLevelChanged()
129 m_previewGenerator
->updateIcons();
130 if (m_selectionManager
!= 0) {
131 m_selectionManager
->reset();
135 void ViewExtensionsFactory::cancelPreviews()
137 m_previewGenerator
->cancelPreviews();
140 void ViewExtensionsFactory::slotShowPreviewChanged()
142 const bool show
= m_controller
->dolphinView()->showPreview();
143 m_previewGenerator
->setPreviewShown(show
);
146 void ViewExtensionsFactory::slotShowHiddenFilesChanged()
148 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel()->sourceModel());
149 KDirLister
* dirLister
= dirModel
->dirLister();
153 const bool show
= m_controller
->dolphinView()->showHiddenFiles();
154 dirLister
->setShowingDotFiles(show
);
156 const KUrl url
= dirLister
->url();
158 dirLister
->openUrl(url
, KDirLister::NoFlags
);
162 void ViewExtensionsFactory::slotSortingChanged(DolphinView::Sorting sorting
)
164 proxyModel()->setSorting(sorting
);
167 void ViewExtensionsFactory::slotSortOrderChanged(Qt::SortOrder order
)
169 proxyModel()->setSortOrder(order
);
172 void ViewExtensionsFactory::slotSortFoldersFirstChanged(bool foldersFirst
)
174 proxyModel()->setSortFoldersFirst(foldersFirst
);
177 void ViewExtensionsFactory::slotNameFilterChanged(const QString
& nameFilter
)
179 proxyModel()->setFilterRegExp(nameFilter
);
182 void ViewExtensionsFactory::requestActivation()
184 m_controller
->requestActivation();
187 DolphinSortFilterProxyModel
* ViewExtensionsFactory::proxyModel() const
189 return static_cast<DolphinSortFilterProxyModel
*>(m_view
->model());
192 #include "viewextensionsfactory.moc"