From f10a0a63e699288f1288c193c49795cae9cb3b40 Mon Sep 17 00:00:00 2001 From: Arno Date: Sat, 16 Oct 2010 12:47:49 +0200 Subject: Created general purpose HoverWidget This commit outsources the HoverWindow to a seperate file and makes it possible to set a pixmap to the window. Also the cursor offset is only calculated once in SmGlobals. --- filestreewidget.cpp | 1 + filestreewidget.h | 1 + hoverwindow.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ hoverwindow.h | 26 ++++++++++++++++++++++++++ seriestreewidget.cpp | 42 ++++-------------------------------------- seriestreewidget.h | 15 +++------------ shemov.pro | 6 ++++-- smglobals.cpp | 15 ++++++++++++++- smglobals.h | 2 ++ 9 files changed, 96 insertions(+), 53 deletions(-) create mode 100644 hoverwindow.cpp create mode 100644 hoverwindow.h diff --git a/filestreewidget.cpp b/filestreewidget.cpp index 4542ea8..a941612 100644 --- a/filestreewidget.cpp +++ b/filestreewidget.cpp @@ -29,6 +29,7 @@ #include "helper.h" #include "pictureviewer.h" #include "filepropertiesdialog.h" +#include "hoverwindow.h" FilesTreeWidget::FilesTreeWidget(QWidget *parent) : QWidget(parent), mSelectedSize(0){ //the view diff --git a/filestreewidget.h b/filestreewidget.h index 86686ca..c340020 100644 --- a/filestreewidget.h +++ b/filestreewidget.h @@ -18,6 +18,7 @@ class FilesTreeModel; class FilesTreeSortModel; class SeriesTreeModel; class PictureViewer; +class HoverWindow; class QContextMenuEvent; class QSpinBox; class QPushButton; diff --git a/hoverwindow.cpp b/hoverwindow.cpp new file mode 100644 index 0000000..59c851e --- /dev/null +++ b/hoverwindow.cpp @@ -0,0 +1,41 @@ +/* + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version + 2 of the License, or (at your option) any later version. +*/ + +#include +#include + +#include "hoverwindow.h" + +HoverWindow::HoverWindow(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f){ + setVisible(false); + setWindowOpacity(0.7); + setStyleSheet("QLabel { background-color: #D6A583; color: black; border-width: 2px; border-style: solid; padding: 4px; }"); + QHBoxLayout *mainLayout = new QHBoxLayout; + mainLayout->setContentsMargins(4, 4, 4, 4); + mLabel = new QLabel; + mainLayout->addWidget(mLabel); + setLayout(mainLayout); +} + +void HoverWindow::setContent(const QString &parent, const QStringList &children){ + QString curText = QString("

%1

").arg(parent); + curText.append("
    "); + int count = qMin(6, children.size()); + int i = 0; + for(i = 0; i < count; ++i){ + curText.append(QString("
  • %1
  • ").arg(children.at(i))); + } + if(i < children.count()){ + curText.append("
  • ...
  • "); + } + curText.append("
"); + mLabel->setText(curText); +} + +void HoverWindow::setPixmap(const QPixmap &pm){ + mLabel->setPixmap(pm); +} diff --git a/hoverwindow.h b/hoverwindow.h new file mode 100644 index 0000000..b020124 --- /dev/null +++ b/hoverwindow.h @@ -0,0 +1,26 @@ +/* + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version + 2 of the License, or (at your option) any later version. +*/ + +#ifndef HOVERWINDOW_H +#define HOVERWINDOW_H + +#include + +class QLabel; + +class HoverWindow : public QWidget { + Q_OBJECT + public: + explicit HoverWindow(QWidget *parent = 0, Qt::WindowFlags f = Qt::FramelessWindowHint); + void setContent(const QString &parent, const QStringList &children); + void setPixmap(const QPixmap &pm); + + private: + QLabel *mLabel; +}; + +#endif // HOVERWINDOW_H diff --git a/seriestreewidget.cpp b/seriestreewidget.cpp index 791fb9e..b5a3feb 100644 --- a/seriestreewidget.cpp +++ b/seriestreewidget.cpp @@ -24,12 +24,6 @@ #include #include #include -#include -#include - -#include -#include -#include #include "seriestreewidget.h" #include "smtreemodel.h" @@ -37,6 +31,7 @@ #include "seriestreemodel.h" #include "filestreemodel.h" #include "helper.h" +#include "hoverwindow.h" SeriesTreeWidget::SeriesTreeWidget(QWidget *parent) : QWidget(parent){ //filter bar @@ -318,12 +313,9 @@ void SeriesTreeWidget::addCover(){ } } -SeriesTreeView::SeriesTreeView(QWidget *parent) : QTreeView(parent), mCursorOffset(0) { +SeriesTreeView::SeriesTreeView(QWidget *parent) : QTreeView(parent) { setAttribute(Qt::WA_Hover); - mHoverWin = new SeriesTreeHoverWindow; - XFixesCursorImage *curImage = XFixesGetCursorImage(QX11Info::display()); - mCursorOffset = curImage->height; - XFree(curImage); + mHoverWin = new HoverWindow; } void SeriesTreeView::contextMenuEvent(QContextMenuEvent *e){ @@ -337,7 +329,7 @@ void SeriesTreeView::contextMenuEvent(QContextMenuEvent *e){ bool SeriesTreeView::event(QEvent *e){ QModelIndex curIdx; QHoverEvent *hEvent = static_cast(e); - QPoint hotSpot(hEvent->pos().x(), hEvent->pos().y() - mCursorOffset); + QPoint hotSpot(hEvent->pos().x(), hEvent->pos().y() - SmGlobals::instance()->cursorOffset()); QPoint globalPos = mapToGlobal(hotSpot); QPoint where = globalPos + QPoint(30, 30); @@ -393,32 +385,6 @@ QStringList SeriesTreeView::children(const QModelIndex &idx) const{ return retval; } -SeriesTreeHoverWindow::SeriesTreeHoverWindow(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f){ - setVisible(false); - setWindowOpacity(0.7); - setStyleSheet("QLabel { background-color: #D6A583; color: black; border-width: 2px; border-style: solid; padding: 4px; }"); - QHBoxLayout *mainLayout = new QHBoxLayout; - mainLayout->setContentsMargins(4, 4, 4, 4); - mLabel = new QLabel; - mainLayout->addWidget(mLabel); - setLayout(mainLayout); -} - -void SeriesTreeHoverWindow::setContent(const QString &parent, const QStringList &children){ - QString curText = QString("

%1

").arg(parent); - curText.append("
    "); - int count = qMin(6, children.size()); - int i = 0; - for(i = 0; i < count; ++i){ - curText.append(QString("
  • %1
  • ").arg(children.at(i))); - } - if(i < children.count()){ - curText.append("
  • ...
  • "); - } - curText.append("
"); - mLabel->setText(curText); -} - SeriesTreeSortModel::SeriesTreeSortModel(QObject *parent) : QSortFilterProxyModel(parent) {} bool SeriesTreeSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const{ diff --git a/seriestreewidget.h b/seriestreewidget.h index 4532ddb..c5d91d4 100644 --- a/seriestreewidget.h +++ b/seriestreewidget.h @@ -22,9 +22,10 @@ class QLabel; class SeriesTreeModel; class SeriesTreeView; class SeriesTreeSortModel; -class SeriesTreeHoverWindow; +class HoverWindow; class FilesTreeModel; class AddCoverDialog; +class HoverWindow; class SeriesTreeWidget : public QWidget { Q_OBJECT @@ -77,19 +78,9 @@ class SeriesTreeView : public QTreeView { private: QStringList children(const QModelIndex &idx) const; QModelIndex mCurHover; - SeriesTreeHoverWindow *mHoverWin; - int mCursorOffset; + HoverWindow *mHoverWin; }; -class SeriesTreeHoverWindow : public QWidget { - Q_OBJECT - public: - explicit SeriesTreeHoverWindow(QWidget *parent = 0, Qt::WindowFlags f = Qt::FramelessWindowHint); - void setContent(const QString &parent, const QStringList &children); - - private: - QLabel *mLabel; -}; class SeriesTreeSortModel : public QSortFilterProxyModel { Q_OBJECT diff --git a/shemov.pro b/shemov.pro index dc3cc47..2015955 100644 --- a/shemov.pro +++ b/shemov.pro @@ -32,7 +32,8 @@ SOURCES = main.cpp \ mappingtablemodel.cpp \ mappingtablewidget.cpp \ newmoviewizard.cpp \ - filepropertiesdialog.cpp + filepropertiesdialog.cpp \ + hoverwindow.cpp HEADERS = listitem.h \ filesystemdirproxy.h \ filesystemwidget.h \ @@ -59,6 +60,7 @@ HEADERS = listitem.h \ mappingtablemodel.h \ mappingtablewidget.h \ newmoviewizard.h \ - filepropertiesdialog.h + filepropertiesdialog.h \ + hoverwindow.h LIBS += -lmagic -lXfixes RESOURCES = shemov.qrc diff --git a/smglobals.cpp b/smglobals.cpp index e70da89..84f0fd8 100644 --- a/smglobals.cpp +++ b/smglobals.cpp @@ -11,6 +11,10 @@ #include #include +#include +#include +#include + #include "smglobals.h" #include "seriestreemodel.h" #include "filestreemodel.h" @@ -80,4 +84,13 @@ PictureViewer *SmGlobals::pictureViewer() { return mPictureViewer; } -SmGlobals::SmGlobals() : mPictureViewer(0) {} +int SmGlobals::cursorOffset() { + if(mCursorOffset == -1){ + XFixesCursorImage *curImage = XFixesGetCursorImage(QX11Info::display()); + mCursorOffset = curImage->height; + XFree(curImage); + } + return mCursorOffset; +} + +SmGlobals::SmGlobals() : mPictureViewer(0), mCursorOffset(-1) {} diff --git a/smglobals.h b/smglobals.h index 50db16a..e1683ee 100644 --- a/smglobals.h +++ b/smglobals.h @@ -20,6 +20,7 @@ class SmGlobals : public QObject { static SmGlobals *instance(); QAbstractItemModel *model(const QString &which); PictureViewer *pictureViewer(); + int cursorOffset(); private: SmGlobals(); @@ -28,6 +29,7 @@ class SmGlobals : public QObject { static SmGlobals *mInstance; QHash mModels; PictureViewer *mPictureViewer; + int mCursorOffset; }; #endif -- cgit v1.2.3-70-g09d2