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... --- fileview.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 94 insertions(+), 8 deletions(-) (limited to 'fileview.cpp') 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); +} -- cgit v1.2.3-70-g09d2