]>
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 <konqmimedata.h>
40 #include <QAbstractProxyModel>
41 #include <QApplication>
48 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
49 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);
73 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
74 // check avoids a division by zero happening on versions before 4.3.1.
75 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
77 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
78 setVerticalScrollMode(QListView::ScrollPerPixel
);
79 setHorizontalScrollMode(QListView::ScrollPerPixel
);
82 // apply the column mode settings to the widget
83 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
84 Q_ASSERT(settings
!= 0);
86 m_viewOptions
= QListView::viewOptions();
88 QFont
font(settings
->fontFamily(), settings
->fontSize());
89 font
.setItalic(settings
->italicFont());
90 font
.setBold(settings
->boldFont());
91 m_viewOptions
.font
= font
;
93 const int iconSize
= settings
->iconSize();
94 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
96 m_viewOptions
.showDecorationSelected
= true;
98 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
99 setItemDelegate(delegate
);
103 connect(this, SIGNAL(viewportEntered()),
104 m_view
->m_controller
, SLOT(emitViewportEntered()));
105 connect(this, SIGNAL(entered(const QModelIndex
&)),
106 this, SLOT(slotEntered(const QModelIndex
&)));
108 //m_dirLister = new DolphinDirLister(); TODO
109 m_dirLister
= new KDirLister();
110 m_dirLister
->setAutoUpdate(true);
111 m_dirLister
->setMainWindow(this);
112 m_dirLister
->setDelayedMimeTypes(true);
113 m_dirLister
->setShowingDotFiles(m_view
->m_controller
->showHiddenFiles());
114 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
115 this, SLOT(generatePreviews(const KFileItemList
&)));
117 m_dolphinModel
= new DolphinModel(this);
118 m_dolphinModel
->setDirLister(m_dirLister
);
119 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
121 m_proxyModel
= new DolphinSortFilterProxyModel(this);
122 m_proxyModel
->setSourceModel(m_dolphinModel
);
124 setModel(m_proxyModel
);
126 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
129 DolphinColumnWidget::~DolphinColumnWidget()
135 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
137 m_viewOptions
.decorationSize
= size
;
141 void DolphinColumnWidget::setActive(bool active
)
143 if (m_active
== active
) {
156 void DolphinColumnWidget::reload()
159 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
162 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
164 if (show
!= m_dirLister
->showingDotFiles()) {
165 m_dirLister
->setShowingDotFiles(show
);
167 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
171 void DolphinColumnWidget::setShowPreview(bool show
)
173 if (show
!= m_showPreview
) {
175 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
179 void DolphinColumnWidget::updateBackground()
181 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
182 if (!m_active
|| !m_view
->m_active
) {
185 QPalette palette
= viewport()->palette();
186 palette
.setColor(viewport()->backgroundRole(), color
);
187 viewport()->setPalette(palette
);
192 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
194 // The name filter of KDirLister does a 'hard' filtering, which
195 // means that only the items are shown where the names match
196 // exactly the filter. This is non-transparent for the user, which
197 // just wants to have a 'soft' filtering: does the name contain
198 // the filter string?
199 QString
adjustedFilter(nameFilter
);
200 adjustedFilter
.insert(0, '*');
201 adjustedFilter
.append('*');
203 m_dirLister
->setNameFilter(adjustedFilter
);
204 m_dirLister
->emitChanges();
207 QString
DolphinColumnWidget::nameFilter() const
209 return m_dirLister
->nameFilter();
212 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
214 if (event
->mimeData()->hasUrls()) {
215 event
->acceptProposedAction();
221 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
223 QListView::dragLeaveEvent(event
);
225 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
227 setDirtyRegion(m_dropRect
);
230 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
232 QListView::dragMoveEvent(event
);
234 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
235 const QModelIndex index
= indexAt(event
->pos());
236 setDirtyRegion(m_dropRect
);
237 m_dropRect
= visualRect(index
);
238 setDirtyRegion(m_dropRect
);
241 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
243 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
244 if (!urls
.isEmpty()) {
245 event
->acceptProposedAction();
246 m_view
->m_controller
->indicateDroppedUrls(urls
,
248 indexAt(event
->pos()),
251 QListView::dropEvent(event
);
255 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
257 if (!m_childUrl
.isEmpty()) {
258 // indicate the shown URL of the next column by highlighting the shown folder item
259 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
260 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
261 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
262 const QRect itemRect
= visualRect(proxyIndex
);
263 QPainter
painter(viewport());
266 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
268 painter
.setPen(Qt::NoPen
);
269 painter
.setBrush(color
);
270 painter
.drawRect(itemRect
);
276 QListView::paintEvent(event
);
278 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
280 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
281 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
285 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
287 m_view
->m_controller
->requestActivation();
289 m_view
->requestActivation(this);
290 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
293 QListView::mousePressEvent(event
);
296 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
298 QListView::keyPressEvent(event
);
300 const QItemSelectionModel
* selModel
= selectionModel();
301 const QModelIndex currentIndex
= selModel
->currentIndex();
302 const bool trigger
= currentIndex
.isValid()
303 && (event
->key() == Qt::Key_Return
)
304 && (selModel
->selectedIndexes().count() <= 1);
306 triggerItem(currentIndex
);
310 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
313 m_view
->requestActivation(this);
314 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
317 QListView::contextMenuEvent(event
);
319 const QModelIndex index
= indexAt(event
->pos());
320 if (index
.isValid() || m_active
) {
321 // Only open a context menu above an item or if the mouse is above
322 // the active column.
323 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
324 m_view
->m_controller
->triggerContextMenuRequest(pos
);
328 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
330 QListView::selectionChanged(selected
, deselected
);
332 QItemSelectionModel
* selModel
= m_view
->selectionModel();
333 selModel
->select(selected
, QItemSelectionModel::Select
);
334 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
336 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
338 const KFileItem item
= m_dolphinModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
339 m_view
->m_controller
->triggerItem(item
);
342 void DolphinColumnWidget::generatePreviews(const KFileItemList
& items
)
344 // TODO: same implementation as in DolphinView; create helper class
345 // for generatePreviews(), showPreview() and isCutItem()
347 if (m_view
->m_controller
->showPreview()) {
348 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
349 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
350 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
354 void DolphinColumnWidget::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
356 // TODO: same implementation as in DolphinView; create helper class
357 // for generatePreviews(), showPreview() and isCutItem()
359 Q_ASSERT(!item
.isNull());
360 if (item
.url().directory() != m_dirLister
->url().path()) {
361 // the preview job is still working on items of an older URL, hence
362 // the item is not part of the directory model anymore
366 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
367 if (idx
.isValid() && (idx
.column() == 0)) {
368 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
369 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
370 KIconEffect iconEffect
;
371 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
372 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
374 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
379 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
381 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
382 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
383 m_view
->m_controller
->emitItemEntered(item
);
386 void DolphinColumnWidget::activate()
388 setFocus(Qt::OtherFocusReason
);
390 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
391 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
392 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
393 if (KGlobalSettings::singleClick()) {
394 connect(this, SIGNAL(clicked(const QModelIndex
&)),
395 this, SLOT(triggerItem(const QModelIndex
&)));
397 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
398 this, SLOT(triggerItem(const QModelIndex
&)));
401 if (!m_childUrl
.isEmpty()) {
402 // assure that the current index is set on the index that represents
404 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
405 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
406 selectionModel()->setCurrentIndex(proxyIndex
, QItemSelectionModel::Current
);
412 void DolphinColumnWidget::deactivate()
416 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
417 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
418 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
419 if (KGlobalSettings::singleClick()) {
420 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
421 this, SLOT(triggerItem(const QModelIndex
&)));
423 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
424 this, SLOT(triggerItem(const QModelIndex
&)));
427 selectionModel()->clear();
431 bool DolphinColumnWidget::isCutItem(const KFileItem
& item
) const
433 // TODO: same implementation as in DolphinView; create helper class
434 // for generatePreviews(), showPreview() and isCutItem()
436 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
437 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
439 const KUrl
& itemUrl
= item
.url();
440 KUrl::List::const_iterator it
= cutUrls
.begin();
441 const KUrl::List::const_iterator end
= cutUrls
.end();
443 if (*it
== itemUrl
) {
452 #include "dolphincolumnwidget.moc"