From a46551d63efd09b34fe33766533f6f5333737f22 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Wed, 28 Feb 2007 19:17:26 +0000 Subject: [PATCH] Jippie: file previews are generated again! Thanks to Fredrik for giving me a hint how to do it (if I would have known that it is so easy, I'd have implemented it already some weeks ago...). It's very nice that the details view now also is able to show previews (Kudos to Interview & KDirModel). svn path=/trunk/KDE/kdebase/apps/; revision=638081 --- src/dolphincontroller.cpp | 11 +++++++++- src/dolphincontroller.h | 10 +++++++++ src/dolphindetailsview.cpp | 2 +- src/dolphiniconsview.cpp | 34 ++++++++++++++++++++++++++----- src/dolphiniconsview.h | 7 +++++++ src/dolphinmainwindow.cpp | 13 ++++++++++++ src/dolphinmainwindow.h | 3 +++ src/dolphinview.cpp | 38 ++++++++++++++++++++++++++++------- src/dolphinview.h | 13 ++++++++++++ src/iconsviewsettingspage.cpp | 4 +--- 10 files changed, 118 insertions(+), 17 deletions(-) diff --git a/src/dolphincontroller.cpp b/src/dolphincontroller.cpp index 8089cfd2b..e1d4c48e3 100644 --- a/src/dolphincontroller.cpp +++ b/src/dolphincontroller.cpp @@ -20,7 +20,8 @@ #include "dolphincontroller.h" DolphinController::DolphinController(QObject* parent) : - QObject(parent) + QObject(parent), + m_showPreview(false) { } @@ -56,6 +57,14 @@ void DolphinController::indicateSortOrderChange(Qt::SortOrder order) emit sortOrderChanged(order); } +void DolphinController::setShowPreview(bool showPreview) +{ + if (m_showPreview != showPreview) { + m_showPreview = showPreview; + emit showPreviewChanged(showPreview); + } +} + void DolphinController::triggerItem(const QModelIndex& index) { emit itemTriggered(index); diff --git a/src/dolphincontroller.h b/src/dolphincontroller.h index bde01c28e..08d12ea57 100644 --- a/src/dolphincontroller.h +++ b/src/dolphincontroller.h @@ -67,6 +67,9 @@ public: void indicateSortOrderChange(Qt::SortOrder order); + void setShowPreview(bool showPreview); + bool showPreview() const { return m_showPreview; } + public slots: void triggerItem(const QModelIndex& index); void indicateSelectionChange(); @@ -102,6 +105,12 @@ signals: /** Is emitted if the sort order has been changed to \a sort order. */ void sortOrderChanged(Qt::SortOrder order); + /** + * Is emitted if the state for showing previews has been + * changed to \a showPreview. + */ + void showPreviewChanged(bool showPreview); + /** * Is emitted if the item with the index \a index should be triggered. * Usually triggering on a directory opens the directory, triggering @@ -113,6 +122,7 @@ signals: void selectionChanged(); private: + bool m_showPreview; KUrl m_url; }; diff --git a/src/dolphindetailsview.cpp b/src/dolphindetailsview.cpp index d227e70c9..1cf68c2e2 100644 --- a/src/dolphindetailsview.cpp +++ b/src/dolphindetailsview.cpp @@ -57,7 +57,7 @@ DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* contr connect(this, SIGNAL(clicked(const QModelIndex&)), controller, SLOT(triggerItem(const QModelIndex&))); - // apply the details mode settings to the widget + // apply the details mode settings to the widget const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings(); assert(settings != 0); diff --git a/src/dolphiniconsview.cpp b/src/dolphiniconsview.cpp index a6da79836..a9328172d 100644 --- a/src/dolphiniconsview.cpp +++ b/src/dolphiniconsview.cpp @@ -34,24 +34,24 @@ DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controlle QListView(parent), m_controller(controller) { - assert(controller != 0); + Q_ASSERT(controller != 0); setViewMode(QListView::IconMode); setResizeMode(QListView::Adjust); connect(this, SIGNAL(clicked(const QModelIndex&)), controller, SLOT(triggerItem(const QModelIndex&))); + connect(controller, SIGNAL(showPreviewChanged(bool)), + this, SLOT(updateGridSize(bool))); // apply the icons mode settings to the widget const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings(); - assert(settings != 0); + Q_ASSERT(settings != 0); - setGridSize(QSize(settings->gridWidth(), settings->gridHeight())); setSpacing(settings->gridSpacing()); m_viewOptions = QListView::viewOptions(); m_viewOptions.font = QFont(settings->fontFamily(), settings->fontSize()); - const int iconSize = settings->iconSize(); - m_viewOptions.decorationSize = QSize(iconSize, iconSize); + updateGridSize(controller->showPreview()); if (settings->arrangement() == QListView::TopToBottom) { setFlow(QListView::LeftToRight); @@ -103,4 +103,28 @@ void DolphinIconsView::dropEvent(QDropEvent* event) } } +void DolphinIconsView::updateGridSize(bool showPreview) +{ + const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings(); + Q_ASSERT(settings != 0); + + int gridWidth = settings->gridWidth(); + int gridHeight = settings->gridHeight(); + int size = settings->iconSize(); + + if (showPreview) { + const int previewSize = settings->previewSize(); + const int diff = previewSize - size; + Q_ASSERT(diff >= 0); + gridWidth += diff; + gridHeight += diff; + + size = previewSize; + } + + + m_viewOptions.decorationSize = QSize(size, size); + setGridSize(QSize(gridWidth, gridHeight)); +} + #include "dolphiniconsview.moc" diff --git a/src/dolphiniconsview.h b/src/dolphiniconsview.h index 91faf344a..22da7de21 100644 --- a/src/dolphiniconsview.h +++ b/src/dolphiniconsview.h @@ -47,6 +47,13 @@ protected: virtual void dragEnterEvent(QDragEnterEvent* event); virtual void dropEvent(QDropEvent* event); +private slots: + /** + * Updates the size of the grid + * depending on the state of \a showPreview. + */ + void updateGridSize(bool showPreview); + private: DolphinController* m_controller; QStyleOptionViewItem m_viewOptions; diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 7a0ff6c6a..edb57ef7f 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -250,6 +250,13 @@ void DolphinMainWindow::slotViewModeChanged() updateViewActions(); } +void DolphinMainWindow::slowShowPreviewChanged() +{ + KToggleAction* showPreviewAction = + static_cast(actionCollection()->action("show_preview")); + showPreviewAction->setChecked(m_activeView->showPreview()); +} + void DolphinMainWindow::slotShowHiddenFilesChanged() { KToggleAction* showHiddenFilesAction = @@ -1290,6 +1297,10 @@ void DolphinMainWindow::updateViewActions() static_cast(actionCollection()->action("show_filter_bar")); showFilterBarAction->setChecked(m_activeView->isFilterBarVisible()); + KToggleAction* showPreviewAction = + static_cast(actionCollection()->action("show_preview")); + showPreviewAction->setChecked(m_activeView->showPreview()); + KToggleAction* showHiddenFilesAction = static_cast(actionCollection()->action("show_hidden_files")); showHiddenFilesAction->setChecked(m_activeView->showHiddenFiles()); @@ -1337,6 +1348,8 @@ void DolphinMainWindow::connectViewSignals(int viewIndex) DolphinView* view = m_view[viewIndex]; connect(view, SIGNAL(modeChanged()), this, SLOT(slotViewModeChanged())); + connect(view, SIGNAL(showPreviewChanged()), + this, SLOT(slowShowPreviewChanged())); connect(view, SIGNAL(showHiddenFilesChanged()), this, SLOT(slotShowHiddenFilesChanged())); connect(view, SIGNAL(sortingChanged(DolphinView::Sorting)), diff --git a/src/dolphinmainwindow.h b/src/dolphinmainwindow.h index 7a71b685c..1b33c8cb0 100644 --- a/src/dolphinmainwindow.h +++ b/src/dolphinmainwindow.h @@ -319,6 +319,9 @@ private slots: /** Updates the state of all 'View' menu actions. */ void slotViewModeChanged(); + /** Updates the state of the 'Show preview' menu action. */ + void slowShowPreviewChanged(); + /** Updates the state of the 'Show hidden files' menu action. */ void slotShowHiddenFilesChanged(); diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index 5ec8c901e..bcbb637ad 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -108,6 +109,8 @@ DolphinView::DolphinView(DolphinMainWindow* mainWindow, this, SLOT(updateStatusBar())); connect(m_dirLister, SIGNAL(completed()), this, SLOT(updateItemCount())); + connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)), + this, SLOT(generatePreviews(const KFileItemList&))); connect(m_dirLister, SIGNAL(infoMessage(const QString&)), this, SLOT(showInfoMessage(const QString&))); connect(m_dirLister, SIGNAL(errorMessage(const QString&)), @@ -204,15 +207,15 @@ void DolphinView::setShowPreview(bool show) ViewProperties props(m_urlNavigator->url()); props.setShowPreview(show); - // TODO: wait until previews are possible with KFileItemDelegate + m_controller->setShowPreview(show); emit showPreviewChanged(); + reload(); } bool DolphinView::showPreview() const { - // TODO: wait until previews are possible with KFileItemDelegate - return true; + return m_controller->showPreview(); } void DolphinView::setShowHiddenFiles(bool show) @@ -456,7 +459,7 @@ KFileItemList DolphinView::selectedItems() const // Our view has a selection, we will map them back to the DirModel // and then fill the KFileItemList. - assert((view != 0) && (view->selectionModel() != 0)); + Q_ASSERT((view != 0) && (view->selectionModel() != 0)); const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection()); KFileItemList itemList; @@ -464,7 +467,7 @@ KFileItemList DolphinView::selectedItems() const const QModelIndexList indexList = selection.indexes(); QModelIndexList::const_iterator end = indexList.end(); for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) { - assert((*it).isValid()); + Q_ASSERT((*it).isValid()); KFileItem* item = m_dirModel->itemForIndex(*it); if (item != 0) { @@ -603,7 +606,11 @@ void DolphinView::loadDirectory(const KUrl& url) emit sortOrderChanged(sortOrder); } - // TODO: handle previews (props.showPreview()) + const bool showPreview = props.showPreview(); + if (showPreview != m_controller->showPreview()) { + m_controller->setShowPreview(showPreview); + emit showPreviewChanged(); + } startDirLister(url); emit urlChanged(url); @@ -682,6 +689,23 @@ void DolphinView::updateItemCount() QTimer::singleShot(0, this, SLOT(restoreContentsPos())); } +void DolphinView::generatePreviews(const KFileItemList& items) +{ + if (m_controller->showPreview()) { + KIO::PreviewJob* job = KIO::filePreview(items, 128); + connect(job, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)), + this, SLOT(showPreview(const KFileItem*, const QPixmap&))); + } +} + +void DolphinView::showPreview(const KFileItem* item, const QPixmap& pixmap) +{ + const QModelIndex idx = m_dirModel->indexForItem(item); + Q_ASSERT(idx.isValid()); + Q_ASSERT(idx.column() == 0); + m_dirModel->setData(idx, pixmap, Qt::DecorationRole); +} + void DolphinView::restoreContentsPos() { int index = 0; @@ -991,7 +1015,7 @@ void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags) QAbstractItemView* DolphinView::itemView() const { - assert((m_iconsView == 0) || (m_detailsView == 0)); + Q_ASSERT((m_iconsView == 0) || (m_detailsView == 0)); if (m_detailsView != 0) { return m_detailsView; } diff --git a/src/dolphinview.h b/src/dolphinview.h index cffa52880..63ceb983a 100644 --- a/src/dolphinview.h +++ b/src/dolphinview.h @@ -404,6 +404,19 @@ private slots: */ void updateItemCount(); + /** + * Generates a preview image for each file item in \a items. + * The current preview settings (maximum size, 'Show Preview' menu) + * are respected. + */ + void generatePreviews(const KFileItemList& items); + + /** + * Replaces the icon of the item \a item by the preview pixmap + * \a pixmap. + */ + void showPreview(const KFileItem* item, const QPixmap& pixmap); + /** * Restores the x- and y-position of the contents if the * current view is part of the history. diff --git a/src/iconsviewsettingspage.cpp b/src/iconsviewsettingspage.cpp index f4c8de577..743ca936b 100644 --- a/src/iconsviewsettingspage.cpp +++ b/src/iconsviewsettingspage.cpp @@ -184,9 +184,7 @@ void IconsViewSettingsPage::applySettings() const int defaultSize = iconSize(m_iconSizeSlider->value()); settings->setIconSize(defaultSize); - int previewSize = //(m_mode == DolphinIconsView::Previews) ? - //iconSize(m_previewSizeSlider->value()) : - defaultSize; + int previewSize = iconSize(m_previewSizeSlider->value()); if (previewSize < defaultSize) { // assure that the preview size is never smaller than the icon size previewSize = defaultSize; -- 2.47.3