]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
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 "dolphinmodel.h"
27 #include "dolphinsortfilterproxymodel.h"
28 #include "dolphinsettings.h"
30 #include "dolphin_columnmodesettings.h"
32 #include <kcolorutils.h>
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
35 #include <kfileitem.h>
36 #include <kio/previewjob.h>
37 #include <kiconeffect.h>
38 #include <kmimetyperesolver.h>
39 #include <konqmimedata.h>
41 #include <QAbstractProxyModel>
42 #include <QApplication>
49 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
50 DolphinColumnView
* columnView
,
65 setMouseTracking(true);
66 viewport()->setAttribute(Qt::WA_Hover
);
67 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
68 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
69 setSelectionBehavior(SelectItems
);
70 setSelectionMode(QAbstractItemView::ExtendedSelection
);
71 setDragDropMode(QAbstractItemView::DragDrop
);
72 setDropIndicatorShown(false);
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_viewOptions
= QListView::viewOptions();
89 QFont
font(settings
->fontFamily(), settings
->fontSize());
90 font
.setItalic(settings
->italicFont());
91 font
.setBold(settings
->boldFont());
92 m_viewOptions
.font
= font
;
94 const int iconSize
= settings
->iconSize();
95 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
97 m_viewOptions
.showDecorationSelected
= true;
99 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
100 setItemDelegate(delegate
);
104 connect(this, SIGNAL(viewportEntered()),
105 m_view
->m_controller
, SLOT(emitViewportEntered()));
106 connect(this, SIGNAL(entered(const QModelIndex
&)),
107 this, SLOT(slotEntered(const QModelIndex
&)));
109 //m_dirLister = new DolphinDirLister(); TODO
110 m_dirLister
= new KDirLister();
111 m_dirLister
->setAutoUpdate(true);
112 m_dirLister
->setMainWindow(this);
113 m_dirLister
->setDelayedMimeTypes(true);
114 m_dirLister
->setShowingDotFiles(m_view
->m_controller
->showHiddenFiles());
115 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
116 this, SLOT(generatePreviews(const KFileItemList
&)));
118 m_dolphinModel
= new DolphinModel(this);
119 m_dolphinModel
->setDirLister(m_dirLister
);
120 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
122 m_proxyModel
= new DolphinSortFilterProxyModel(this);
123 m_proxyModel
->setSourceModel(m_dolphinModel
);
125 setModel(m_proxyModel
);
126 new KMimeTypeResolver(this, m_dolphinModel
);
128 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
131 DolphinColumnWidget::~DolphinColumnWidget()
137 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
139 m_viewOptions
.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 if (show
!= m_showPreview
) {
177 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
181 void DolphinColumnWidget::updateBackground()
183 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
184 if (!m_active
|| !m_view
->m_active
) {
187 QPalette palette
= viewport()->palette();
188 palette
.setColor(viewport()->backgroundRole(), color
);
189 viewport()->setPalette(palette
);
194 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
196 m_proxyModel
->setFilterRegExp(nameFilter
);
199 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
201 if (event
->mimeData()->hasUrls()) {
202 event
->acceptProposedAction();
208 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
210 QListView::dragLeaveEvent(event
);
212 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
214 setDirtyRegion(m_dropRect
);
217 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
219 QListView::dragMoveEvent(event
);
221 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
222 const QModelIndex index
= indexAt(event
->pos());
223 setDirtyRegion(m_dropRect
);
224 m_dropRect
= visualRect(index
);
225 setDirtyRegion(m_dropRect
);
228 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
230 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
231 if (!urls
.isEmpty()) {
232 const QModelIndex index
= indexAt(event
->pos());
233 if (index
.isValid()) {
234 const KFileItem item
= itemForIndex(index
);
235 m_view
->m_controller
->indicateDroppedUrls(urls
,
239 event
->acceptProposedAction();
242 QListView::dropEvent(event
);
246 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
248 if (!m_childUrl
.isEmpty()) {
249 // indicate the shown URL of the next column by highlighting the shown folder item
250 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
251 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
252 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
253 const QRect itemRect
= visualRect(proxyIndex
);
254 QPainter
painter(viewport());
257 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
259 painter
.setPen(Qt::NoPen
);
260 painter
.setBrush(color
);
261 painter
.drawRect(itemRect
);
267 QListView::paintEvent(event
);
269 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
271 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
272 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
276 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
278 m_view
->m_controller
->requestActivation();
280 m_view
->requestActivation(this);
281 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
284 QListView::mousePressEvent(event
);
287 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
289 QListView::keyPressEvent(event
);
291 const QItemSelectionModel
* selModel
= selectionModel();
292 const QModelIndex currentIndex
= selModel
->currentIndex();
293 const bool trigger
= currentIndex
.isValid()
294 && (event
->key() == Qt::Key_Return
)
295 && (selModel
->selectedIndexes().count() <= 1);
297 triggerItem(currentIndex
);
301 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
304 m_view
->requestActivation(this);
305 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
308 QListView::contextMenuEvent(event
);
310 const QModelIndex index
= indexAt(event
->pos());
311 if (index
.isValid() || m_active
) {
312 // Only open a context menu above an item or if the mouse is above
313 // the active column.
314 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
315 m_view
->m_controller
->triggerContextMenuRequest(pos
);
319 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
321 QListView::selectionChanged(selected
, deselected
);
323 QItemSelectionModel
* selModel
= m_view
->selectionModel();
324 selModel
->select(selected
, QItemSelectionModel::Select
);
325 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
327 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
329 const KFileItem item
= itemForIndex(index
);
330 m_view
->m_controller
->triggerItem(item
);
333 void DolphinColumnWidget::generatePreviews(const KFileItemList
& items
)
335 // TODO: same implementation as in DolphinView; create helper class
336 // for generatePreviews(), showPreview() and isCutItem()
338 if (m_view
->m_controller
->showPreview()) {
339 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
340 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
341 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
345 void DolphinColumnWidget::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
347 // TODO: same implementation as in DolphinView; create helper class
348 // for generatePreviews(), showPreview() and isCutItem()
350 Q_ASSERT(!item
.isNull());
351 if (item
.url().directory() != m_dirLister
->url().path()) {
352 // the preview job is still working on items of an older URL, hence
353 // the item is not part of the directory model anymore
357 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
358 if (idx
.isValid() && (idx
.column() == 0)) {
359 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
360 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
361 KIconEffect iconEffect
;
362 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
363 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
365 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
370 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
372 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
373 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
374 m_view
->m_controller
->emitItemEntered(item
);
377 void DolphinColumnWidget::activate()
379 setFocus(Qt::OtherFocusReason
);
381 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
382 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
383 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
384 if (KGlobalSettings::singleClick()) {
385 connect(this, SIGNAL(clicked(const QModelIndex
&)),
386 this, SLOT(triggerItem(const QModelIndex
&)));
388 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
389 this, SLOT(triggerItem(const QModelIndex
&)));
392 if (!m_childUrl
.isEmpty()) {
393 // assure that the current index is set on the index that represents
395 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
396 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
397 selectionModel()->setCurrentIndex(proxyIndex
, QItemSelectionModel::Current
);
403 void DolphinColumnWidget::deactivate()
407 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
408 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
409 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
410 if (KGlobalSettings::singleClick()) {
411 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
412 this, SLOT(triggerItem(const QModelIndex
&)));
414 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
415 this, SLOT(triggerItem(const QModelIndex
&)));
418 selectionModel()->clear();
422 bool DolphinColumnWidget::isCutItem(const KFileItem
& item
) const
424 // TODO: same implementation as in DolphinView; create helper class
425 // for generatePreviews(), showPreview() and isCutItem()
427 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
428 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
430 const KUrl
& itemUrl
= item
.url();
431 KUrl::List::const_iterator it
= cutUrls
.begin();
432 const KUrl::List::const_iterator end
= cutUrls
.end();
434 if (*it
== itemUrl
) {
443 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
445 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
446 return m_dolphinModel
->itemForIndex(dirIndex
);
450 #include "dolphincolumnwidget.moc"