diff options
author | Arno <am@disconnect.de> | 2010-10-17 12:48:20 +0200 |
---|---|---|
committer | Arno <am@disconnect.de> | 2010-10-17 12:48:20 +0200 |
commit | 19674f64699ba883f24a97a04505ca2c7f12634e (patch) | |
tree | 0abe1e00310b625c756a90fea06b42c96d048c0f /fileview.cpp | |
parent | fe9e348cc2ec4990b0c81179b0507f4a1df54c95 (diff) | |
download | SheMov-19674f64699ba883f24a97a04505ca2c7f12634e.tar.gz SheMov-19674f64699ba883f24a97a04505ca2c7f12634e.tar.bz2 SheMov-19674f64699ba883f24a97a04505ca2c7f12634e.zip |
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...
Diffstat (limited to 'fileview.cpp')
-rw-r--r-- | fileview.cpp | 102 |
1 files changed, 94 insertions, 8 deletions
diff --git a/fileview.cpp b/fileview.cpp index 7853504..2da9bc6 100644 --- a/fileview.cpp +++ b/fileview.cpp @@ -15,11 +15,22 @@ #include <QSortFilterProxyModel> #include <QAction> #include <QApplication> +#include <QEvent> +#include <QSettings> +#include <QModelIndex> +#include <QFileInfo> +#include <QVariant> +#include <QList> +#include <QDir> #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<QSortFilterProxyModel*>(model()); - QFileSystemModel *model = static_cast<QFileSystemModel*>(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<QHoverEvent*>(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<QVariant> 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); +} |