1 /***************************************************************************
2 * Copyright (C) 2007 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 "dolphincolumnwidget.h"
22 #include "dolphinmodel.h"
23 #include "dolphincolumnview.h"
24 #include "dolphincontroller.h"
25 #include "dolphindirlister.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "dolphinsettings.h"
28 #include "dolphin_columnmodesettings.h"
29 #include "draganddrophelper.h"
31 #include <kcolorscheme.h>
32 #include <kdirlister.h>
33 #include <kfileitem.h>
34 #include <kio/previewjob.h>
35 #include <kiconeffect.h>
37 #include <kmimetyperesolver.h>
38 #include <konqmimedata.h>
40 #include "iconmanager.h"
42 #include <QApplication>
47 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
48 DolphinColumnView
* columnView
,
64 setMouseTracking(true);
65 viewport()->setAttribute(Qt::WA_Hover
);
66 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
67 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
68 setSelectionBehavior(SelectItems
);
69 setSelectionMode(QAbstractItemView::ExtendedSelection
);
70 setDragDropMode(QAbstractItemView::DragDrop
);
71 setDropIndicatorShown(false);
72 setSelectionRectVisible(true);
74 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
75 // check avoids a division by zero happening on versions before 4.3.1.
76 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
78 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
79 setVerticalScrollMode(QListView::ScrollPerPixel
);
80 setHorizontalScrollMode(QListView::ScrollPerPixel
);
83 // apply the column mode settings to the widget
84 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
85 Q_ASSERT(settings
!= 0);
87 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
88 m_font
.setItalic(settings
->italicFont());
89 m_font
.setBold(settings
->boldFont());
91 const int iconSize
= settings
->iconSize();
92 m_decorationSize
= QSize(iconSize
, iconSize
);
94 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
95 setItemDelegate(delegate
);
99 connect(this, SIGNAL(viewportEntered()),
100 m_view
->m_controller
, SLOT(emitViewportEntered()));
101 connect(this, SIGNAL(entered(const QModelIndex
&)),
102 this, SLOT(slotEntered(const QModelIndex
&)));
104 //m_dirLister = new DolphinDirLister(); TODO
105 m_dirLister
= new KDirLister();
106 m_dirLister
->setAutoUpdate(true);
107 m_dirLister
->setMainWindow(this);
108 m_dirLister
->setDelayedMimeTypes(true);
109 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
110 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
112 m_dolphinModel
= new DolphinModel(this);
113 m_dolphinModel
->setDirLister(m_dirLister
);
114 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
116 m_proxyModel
= new DolphinSortFilterProxyModel(this);
117 m_proxyModel
->setSourceModel(m_dolphinModel
);
118 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
120 setModel(m_proxyModel
);
121 new KMimeTypeResolver(this, m_dolphinModel
);
122 m_iconManager
= new IconManager(this, m_proxyModel
);
123 m_iconManager
->setShowPreview(m_view
->m_controller
->dolphinView()->showPreview());
125 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
128 DolphinColumnWidget::~DolphinColumnWidget()
132 delete m_dolphinModel
;
134 m_dirLister
= 0; // deleted by m_dolphinModel
137 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
139 m_decorationSize
= size
;
143 void DolphinColumnWidget::setActive(bool active
)
145 if (m_active
== active
) {
158 void DolphinColumnWidget::reload()
161 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
164 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
166 if (show
!= m_dirLister
->showingDotFiles()) {
167 m_dirLister
->setShowingDotFiles(show
);
169 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
173 void DolphinColumnWidget::setShowPreview(bool show
)
175 kDebug() << "-------------- column widget: show" << show
;
176 m_iconManager
->setShowPreview(show
);
179 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
182 void DolphinColumnWidget::updateBackground()
184 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
185 // cleaning up the cut-indication of DolphinColumnWidget with the code from
186 // DolphinView a common helper-class should be available which can be shared
187 // by all view implementations -> no hardcoded value anymore
188 const QPalette::ColorRole role
= viewport()->backgroundRole();
189 QColor color
= viewport()->palette().color(role
);
190 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
192 QPalette palette
= viewport()->palette();
193 palette
.setColor(role
, color
);
194 viewport()->setPalette(palette
);
199 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
201 m_proxyModel
->setFilterRegExp(nameFilter
);
205 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
207 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
208 viewOptions
.font
= m_font
;
209 viewOptions
.decorationSize
= m_decorationSize
;
210 viewOptions
.showDecorationSelected
= true;
214 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
216 DragAndDropHelper::startDrag(this, supportedActions
);
219 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
221 if (event
->mimeData()->hasUrls()) {
222 event
->acceptProposedAction();
228 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
230 QListView::dragLeaveEvent(event
);
232 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
234 setDirtyRegion(m_dropRect
);
237 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
239 QListView::dragMoveEvent(event
);
241 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
242 const QModelIndex index
= indexAt(event
->pos());
243 setDirtyRegion(m_dropRect
);
245 m_dropRect
.setSize(QSize()); // set as invalid
246 if (index
.isValid()) {
247 const KFileItem item
= itemForIndex(index
);
248 if (!item
.isNull() && item
.isDir()) {
249 m_dropRect
= visualRect(index
);
252 setDirtyRegion(m_dropRect
);
254 if (event
->mimeData()->hasUrls()) {
255 // accept url drops, independently from the destination item
256 event
->acceptProposedAction();
260 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
262 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
263 if (!urls
.isEmpty()) {
264 const QModelIndex index
= indexAt(event
->pos());
265 const KFileItem item
= itemForIndex(index
);
266 m_view
->m_controller
->indicateDroppedUrls(urls
,
269 event
->acceptProposedAction();
271 QListView::dropEvent(event
);
275 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
277 if (!m_childUrl
.isEmpty()) {
278 // indicate the shown URL of the next column by highlighting the shown folder item
279 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
280 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
281 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
282 const QRect itemRect
= visualRect(proxyIndex
);
283 QPainter
painter(viewport());
286 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
288 painter
.setPen(Qt::NoPen
);
289 painter
.setBrush(color
);
290 painter
.drawRect(itemRect
);
296 QListView::paintEvent(event
);
298 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
300 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
301 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
305 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
307 m_view
->m_controller
->requestActivation();
309 m_view
->requestActivation(this);
310 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
313 QListView::mousePressEvent(event
);
316 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
318 QListView::keyPressEvent(event
);
320 const QItemSelectionModel
* selModel
= selectionModel();
321 const QModelIndex currentIndex
= selModel
->currentIndex();
322 const bool trigger
= currentIndex
.isValid()
323 && (event
->key() == Qt::Key_Return
)
324 && (selModel
->selectedIndexes().count() <= 1);
326 triggerItem(currentIndex
);
330 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
333 m_view
->requestActivation(this);
334 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
337 QListView::contextMenuEvent(event
);
339 const QModelIndex index
= indexAt(event
->pos());
340 if (index
.isValid() || m_active
) {
341 // Only open a context menu above an item or if the mouse is above
342 // the active column.
343 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
344 m_view
->m_controller
->triggerContextMenuRequest(pos
);
348 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
350 QListView::selectionChanged(selected
, deselected
);
352 QItemSelectionModel
* selModel
= m_view
->selectionModel();
353 selModel
->select(selected
, QItemSelectionModel::Select
);
354 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
357 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
359 const KFileItem item
= itemForIndex(index
);
360 m_view
->m_controller
->triggerItem(item
);
363 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
365 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
366 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
367 m_view
->m_controller
->emitItemEntered(item
);
370 void DolphinColumnWidget::activate()
372 setFocus(Qt::OtherFocusReason
);
374 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
375 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
376 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
377 if (KGlobalSettings::singleClick()) {
378 connect(this, SIGNAL(clicked(const QModelIndex
&)),
379 this, SLOT(triggerItem(const QModelIndex
&)));
381 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
382 this, SLOT(triggerItem(const QModelIndex
&)));
388 void DolphinColumnWidget::deactivate()
392 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
393 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
394 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
395 if (KGlobalSettings::singleClick()) {
396 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
397 this, SLOT(triggerItem(const QModelIndex
&)));
399 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
400 this, SLOT(triggerItem(const QModelIndex
&)));
403 selectionModel()->clear();
407 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
409 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
410 return m_dolphinModel
->itemForIndex(dirIndex
);
414 #include "dolphincolumnwidget.moc"