fix tooltip to DP

This commit is contained in:
arcan1s 2014-11-09 08:22:05 +03:00
parent 64fdc4bebe
commit b53b1111f9
2 changed files with 56 additions and 57 deletions

View File

@ -24,7 +24,6 @@
#include <Plasma/Containment>
#include <Plasma/Corona>
#include <Plasma/Theme>
#include <Plasma/ToolTipContent>
#include <Plasma/ToolTipManager>
#include <QBuffer>
#include <QDebug>
@ -64,6 +63,18 @@ int CustomPlasmaLabel::getNumber()
}
void CustomPlasmaLabel::enterEvent(QEvent *event)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Event" << event->type();
if (event->type() == QEvent::Enter)
widget->paintTooltip(number);
emit(QLabel::enterEvent(event));
}
void CustomPlasmaLabel::mousePressEvent(QMouseEvent *event)
{
if (debug) qDebug() << PDEBUG;
@ -111,14 +122,18 @@ void DesktopPanel::init()
layout->setContentsMargins(1, 1, 1, 1);
setLayout(layout);
// tooltip
toolTip = Plasma::ToolTipContent();
toolTipScene = new QGraphicsScene();
toolTipView = new QGraphicsView(toolTipScene);
toolTipView->setStyleSheet(QString("background: transparent"));
toolTipView->setContentsMargins(0, 0, 0, 0);
toolTipView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
toolTipView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Plasma::ToolTipManager::self()->setContent(this, toolTip);
// read variables
configChanged();
timer = new QTimer(this);
timer->setSingleShot(false);
timer->setInterval(2000);
connect(timer, SIGNAL(timeout()), this, SLOT(updateTooltip()));
connect(this, SIGNAL(releaseVisualFocus()), this, SLOT(paintTooltip()));
timer->start();
connect(this, SIGNAL(activate()), this, SLOT(changePanelsState()));
connect(KWindowSystem::self(), SIGNAL(currentDesktopChanged(int)), this, SLOT(updateText(int)));
}
@ -281,10 +296,33 @@ void DesktopPanel::changePanelsState()
}
void DesktopPanel::paintTooltip()
void DesktopPanel::paintTooltip(const int active)
{
if (debug) qDebug() << PDEBUG;
qDebug() << PDEBUG;
if (active == activeTooltip) return;
// prepare
activeTooltip = active;
DesktopWindowsInfo info = getInfoByDesktop(active + 1);
toolTipView->resize(info.desktop.width() * 1.01, info.desktop.height() * 1.03);
toolTipScene->clear();
toolTipScene->setBackgroundBrush(QBrush(Qt::NoBrush));
QPen pen = QPen();
pen.setWidthF(2.0 * info.desktop.width() / 400.0);
for (int i=0; i<info.windows.count(); i++) {
toolTipScene->addLine(info.windows[i].left(), info.windows[i].bottom(),
info.windows[i].left(), info.windows[i].top(), pen);
toolTipScene->addLine(info.windows[i].left(), info.windows[i].top(),
info.windows[i].right(), info.windows[i].top(), pen);
toolTipScene->addLine(info.windows[i].right(), info.windows[i].top(),
info.windows[i].right(), info.windows[i].bottom(), pen);
toolTipScene->addLine(info.windows[i].right(), info.windows[i].bottom(),
info.windows[i].left(), info.windows[i].bottom(), pen);
}
toolTip.setImage(QPixmap::grabWidget(toolTipView).scaledToWidth(configuration[QString("tooltipWidth")].toInt()));
Plasma::ToolTipManager::self()->setContent(this, toolTip);
}
@ -330,49 +368,6 @@ void DesktopPanel::updateText(const int active)
}
void DesktopPanel::updateTooltip()
{
if (debug) qDebug() << PDEBUG;
if (configuration[QString("tooltip")].toInt() != 2) return;
for (int i=0; i<proxyWidgets.count(); i++) {
QGraphicsScene *toolTipScene = new QGraphicsScene();
toolTipScene->setBackgroundBrush(QBrush(Qt::NoBrush));
QGraphicsView *toolTipView = new QGraphicsView(toolTipScene);
toolTipView->setStyleSheet(QString("background: transparent"));
toolTipView->setContentsMargins(0, 0, 0, 0);
toolTipView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
toolTipView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// paint
DesktopWindowsInfo info = getInfoByDesktop(i + 1);
toolTipView->resize(info.desktop.width() * 1.01, info.desktop.height() * 1.03);
QPen pen = QPen();
pen.setWidthF(2.0 * info.desktop.width() / 400.0);
for (int i=0; i<info.windows.count(); i++) {
toolTipScene->addLine(info.windows[i].left(), info.windows[i].bottom(),
info.windows[i].left(), info.windows[i].top(), pen);
toolTipScene->addLine(info.windows[i].left(), info.windows[i].top(),
info.windows[i].right(), info.windows[i].top(), pen);
toolTipScene->addLine(info.windows[i].right(), info.windows[i].top(),
info.windows[i].right(), info.windows[i].bottom(), pen);
toolTipScene->addLine(info.windows[i].right(), info.windows[i].bottom(),
info.windows[i].left(), info.windows[i].bottom(), pen);
}
// convert
QPixmap pixmap = QPixmap::grabWidget(toolTipView);
QByteArray byteArray;
QBuffer buffer(&byteArray);
pixmap.scaledToWidth(configuration[QString("tooltipWidth")].toInt()).save(&buffer, "PNG");
QString url = QString("<html><style type=\"text/css\">body {margin: 0; padding: 0;}</style><body><img src=\"data:image/png;base64,") +
byteArray.toBase64() +
QString("\"/></body></html>");
proxyWidgets[i]->setToolTip(url);
delete toolTipView;
delete toolTipScene;
}
}
// configuration interface
void DesktopPanel::createConfigurationInterface(KConfigDialog *parent)
{

View File

@ -20,7 +20,7 @@
#define DESKTOP_PANEL_H
#include <Plasma/Applet>
#include <Plasma/DataEngine>
#include <Plasma/ToolTipContent>
#include <QLabel>
#include <ui_appearance.h>
@ -30,7 +30,7 @@
class QGraphicsGridLayout;
class QGraphicsProxyWidget;
class QTimer;
class QGraphicsScene;
class DesktopPanel;
@ -46,6 +46,7 @@ public:
int getNumber();
protected:
void enterEvent(QEvent *event);
void mousePressEvent(QMouseEvent *event);
private:
@ -79,15 +80,14 @@ public slots:
void configChanged();
// events
void changePanelsState();
void paintTooltip(const int active);
void setCurrentDesktop(const int number);
private slots:
void reinit();
void paintTooltip();
void setFontFormating();
void setFormating();
void updateText(const int active);
void updateTooltip();
protected:
void createConfigurationInterface(KConfigDialog *parent);
@ -100,9 +100,13 @@ private:
QGraphicsGridLayout *layout;
QList<QGraphicsProxyWidget *> proxyWidgets;
QList<CustomPlasmaLabel *> labels;
QTimer *timer;
// tooltip
Plasma::ToolTipContent toolTip;
QGraphicsScene *toolTipScene;
QGraphicsView *toolTipView;
// debug
bool debug;
int activeTooltip = -1;
int oldState;
// configuration interface
Ui::AppearanceWidget uiAppConfig;