From 19674f64699ba883f24a97a04505ca2c7f12634e Mon Sep 17 00:00:00 2001 From: Arno Date: Sun, 17 Oct 2010 12:48:20 +0200 Subject: Implemented hovering in FilesystemWidget Hovering over dirs and pictures show either the content of the directory or a scaled image of the picture. This is a strange commit, though. When the archive was the last opened tab the program crashes when changing to FilesystemView. I don't have the slightest clue, why, so I simply removed setting the last opened tab on startup. Also there is some strange behavior regarding the position of the HoverWindow and what Qt thinks the global position is... --- filestreemodel.h | 2 +- filestreewidget.cpp | 3 ++ fileview.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++++----- fileview.h | 6 +++- hoverwindow.cpp | 18 ++++++++++ hoverwindow.h | 3 ++ shemov.cpp | 4 --- 7 files changed, 124 insertions(+), 14 deletions(-) diff --git a/filestreemodel.h b/filestreemodel.h index da50d84..a620312 100644 --- a/filestreemodel.h +++ b/filestreemodel.h @@ -21,7 +21,7 @@ class SeriesTreeModel; class FilesTreeModel : public SmTreeModel { Q_OBJECT public: - enum CustomRoles { FileNameRole = Qt::UserRole + 1, FullPathRole = Qt::UserRole + 2, SizeRole = Qt::UserRole + 3, DvdNoRole = Qt::UserRole + 4, SizeDisplayRole = Qt::UserRole + 5, FileTypeRole = Qt::UserRole + 6, Md5SumRole = Qt::UserRole + 7, PartNoRole = Qt::UserRole + 8, SeriesPartIdRole = Qt::UserRole + 9, QualityRole = Qt::UserRole + 10, FilesIdRole = Qt::UserRole + 11, SeriesPartRole = Qt::UserRole + 12, DisplayNameRole = Qt::UserRole + 13, SizeDurationRole = Qt::UserRole + 14, SeriesNameRole = Qt::UserRole + 15 }; + enum CustomRoles { FileNameRole = Qt::UserRole + 1, FullPathRole = Qt::UserRole + 2, SizeRole = Qt::UserRole + 3, DvdNoRole = Qt::UserRole + 4, SizeDisplayRole = Qt::UserRole + 5, FileTypeRole = Qt::UserRole + 6, Md5SumRole = Qt::UserRole + 7, PartNoRole = Qt::UserRole + 8, SeriesPartIdRole = Qt::UserRole + 9, QualityRole = Qt::UserRole + 10, FilesIdRole = Qt::UserRole + 11, SeriesPartRole = Qt::UserRole + 12, DisplayNameRole = Qt::UserRole + 13, SizeDurationRole = Qt::UserRole + 14, SeriesNameRole = Qt::UserRole + 15 }; enum FileTypes { Movie = 1, FrontCover = 2, BackCover = 3, GeneralCover = 4 }; enum Fields { FileName = 0, PartNo = 1, SizeDisplay = 2, Quality = 3, DvdNo = 4, FullPath = 5, Size = 6, FileType = 7, Md5Sum = 8, SeriesPartId = 9, FilesId = 10, SeriesPart = 11, DisplayName = 12, SizeDuration = 13, SeriesName = 14 }; enum Mode { Normal = 0, Archived = 1, Local = 2 }; diff --git a/filestreewidget.cpp b/filestreewidget.cpp index 1181c21..dad3724 100644 --- a/filestreewidget.cpp +++ b/filestreewidget.cpp @@ -295,6 +295,9 @@ bool FilesTreeView::event(QEvent *e){ } QModelIndex curIdx; QHoverEvent *hEvent = static_cast(e); + if(!hEvent){ + return true; + } QPoint hotSpot(hEvent->pos().x(), hEvent->pos().y() - SmGlobals::instance()->cursorOffset()); QPoint globalPos = mapToGlobal(hotSpot); QPoint where = globalPos + QPoint(30, 0); diff --git a/fileview.cpp b/fileview.cpp index 7853504..2da9bc6 100644 --- a/fileview.cpp +++ b/fileview.cpp @@ -15,11 +15,22 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include #include "fileview.h" #include "messagedialog.h" +#include "hoverwindow.h" +#include "smglobals.h" +#include "helper.h" FileView::FileView(QWidget *parent) : QTreeView(parent), mDeleteA(0) { + setAttribute(Qt::WA_Hover); setRootIsDecorated(false); QString title = QString("%1 - %2"); QString markTitle = title.arg(qApp->applicationName(), tr("Mark files")); @@ -28,6 +39,7 @@ FileView::FileView(QWidget *parent) : QTreeView(parent), mDeleteA(0) { QString folderTitle = title.arg(qApp->applicationName(), tr("Create folder")); mCreateFolderDialog = new MessageDialog(tr("Enter folder name"), folderTitle, this); connect(mCreateFolderDialog, SIGNAL(accepted()), this, SLOT(doCreateFolder())); + mHoverWin = new HoverWindow; } void FileView::markFiles(){ @@ -42,14 +54,6 @@ void FileView::createFolder(){ mCreateFolderDialog->show(); } -/*void FileView::refresh(){ - QSortFilterProxyModel *proxy = static_cast(model()); - QFileSystemModel *model = static_cast(proxy->sourceModel()); - QModelIndex root = rootIndex(); - QModelIndex real = proxy->mapToSource(root); - model->refresh(root); -}*/ - void FileView::doMark(){ int rowCount = model()->rowCount(rootIndex()); QString sRegex = mMarkDialog->text(); @@ -134,3 +138,85 @@ void FileView::resizeEvent(QResizeEvent *e){ } } +bool FileView::event(QEvent *e){ + QSettings s; + if(!s.value("ui/hoverpics").toBool()){ + return true; + } + + if(e->type() == QEvent::HoverLeave){ + mHoverWin->setVisible(false); + mCurHover = QModelIndex(); + return true; + } + + QHoverEvent *hEvent = static_cast(e); + QPoint hotSpot(hEvent->pos().x(), hEvent->pos().y() - SmGlobals::instance()->cursorOffset()); + QModelIndex curIdx = indexAt(hotSpot); + + if(!curIdx.isValid()){ + return true; + } + if(curIdx.column() != 0){ + mHoverWin->setVisible(false); + return true; + } + + QFileInfo fi(curIdx.data(QFileSystemModel::FilePathRole).toString()); + if(!fi.exists()){ + return true; + } + + QList hoverData; + if(fi.isDir()){ + hoverData << fi.fileName(); + QDir curDir(fi.absoluteFilePath()); + QStringList files = curDir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot, QDir::Name); + hoverData << files; + }else if(fi.isFile()){ + QString mimeType = Helper::mimeType(fi.absoluteFilePath()); + if(mimeType.startsWith("image")){ + QPixmap pm(fi.absoluteFilePath()); + hoverData << pm; + }else{ + mHoverWin->setVisible(false); + return true; + } + }else{ + mHoverWin->setVisible(false); + return true; + } + + QPoint globalPos = mapToGlobal(hotSpot); + QPoint where = globalPos + QPoint(30, 0); + if(e->type() == QEvent::HoverEnter){ + mCurHover = curIdx; + mHoverWin->setData(hoverData); + if(mHoverWin->pixmapHeight()){ + where = QPoint(where.x(), where.y() - mHoverWin->pixmapHeight() / 2); + } + mHoverWin->setPos(where); + mHoverWin->setVisible(true); + return true; + } + if(e->type() == QEvent::HoverMove){ + if(curIdx != mCurHover){ + mCurHover = curIdx; + mHoverWin->setData(hoverData); + if(mHoverWin->pixmapHeight()){ + where = QPoint(where.x(), where.y() - mHoverWin->pixmapHeight() / 2); + } + mHoverWin->setVisible(false); + mHoverWin->setPos(where); + mHoverWin->setVisible(true); + return true; + }else{ + if(mHoverWin->pixmapHeight()){ + where = QPoint(where.x(), where.y() - mHoverWin->pixmapHeight() / 2); + } + mHoverWin->setPos(where); + return true; + } + } + return QTreeView::event(e); +} diff --git a/fileview.h b/fileview.h index 2bf81b2..6e6e303 100644 --- a/fileview.h +++ b/fileview.h @@ -16,6 +16,8 @@ class QResizeEvent; class MessageDialog; class QModelIndex; class QAction; +class HoverWindow; +class QEvent; class FileView : public QTreeView { Q_OBJECT @@ -33,7 +35,6 @@ class FileView : public QTreeView { void markFiles(); void unmarkFiles(); void createFolder(); - //void refresh(); private slots: void doMark(); @@ -43,11 +44,14 @@ class FileView : public QTreeView { virtual void contextMenuEvent(QContextMenuEvent *e); virtual void keyPressEvent(QKeyEvent *e); virtual void resizeEvent(QResizeEvent *e); + virtual bool event(QEvent *event); private: MessageDialog *mMarkDialog; MessageDialog *mCreateFolderDialog; QAction *mDeleteA; + HoverWindow *mHoverWin; + QModelIndex mCurHover; }; #endif diff --git a/hoverwindow.cpp b/hoverwindow.cpp index 100f278..7f2e4af 100644 --- a/hoverwindow.cpp +++ b/hoverwindow.cpp @@ -49,6 +49,24 @@ void HoverWindow::setPixmap(const QPixmap &pm){ mLabel->setPixmap(curPm); } +void HoverWindow::setData(const QList &data){ + if(data.isEmpty()){ + return; + } + if(data.at(0).canConvert(QVariant::Pixmap)){ + setPixmap(data.at(0).value()); + return; + } + if(data.size() != 2){ + return; + } + QStringList dataList = data.at(1).toStringList(); + if(dataList.isEmpty()){ + dataList << tr("<empty>"); + } + setContent(data.at(0).toString(), dataList); +} + int HoverWindow::pixmapHeight() const{ const QPixmap *pm = mLabel->pixmap(); return pm ? pm->height() : 0; diff --git a/hoverwindow.h b/hoverwindow.h index 4223f9a..49aec40 100644 --- a/hoverwindow.h +++ b/hoverwindow.h @@ -9,6 +9,8 @@ #define HOVERWINDOW_H #include +#include +#include class QLabel; @@ -18,6 +20,7 @@ class HoverWindow : public QWidget { explicit HoverWindow(QWidget *parent = 0, Qt::WindowFlags f = Qt::FramelessWindowHint); void setContent(const QString &parent, const QStringList &children); void setPixmap(const QPixmap &pm); + void setData(const QList &data); int pixmapHeight() const; void setPos(const QPoint &cursorPos); diff --git a/shemov.cpp b/shemov.cpp index 8294900..f00caf5 100644 --- a/shemov.cpp +++ b/shemov.cpp @@ -606,10 +606,6 @@ void SheMov::readSettings(){ if(winState){ setWindowState(Qt::WindowMaximized); } - int currentTab = s.value("ui/selectedtab", 0).toInt(); - if(currentTab < mTab->count() && currentTab != mTab->currentIndex()){ - mTab->setCurrentIndex(currentTab); - } int filesMode = s.value("ui/filestreemode", FilesTreeModel::Normal).toInt(); switch(filesMode){ case FilesTreeModel::Normal: -- cgit v1.2.3-70-g09d2