/* 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 #include #include #include #include #include #include #include #include #include #include #include #include #include "hoverwindow.h" #include "smglobals.h" HoverWindow::HoverWindow(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f), mAlignCenter(false), mDesktopHeight(-1){ setVisible(false); QSettings s; int opacity = s.value("ui/hoveropacity", 7).toInt(); setWindowOpacity(opacity / 10.0); setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); setStyleSheet("QLabel { background-color: #D6A583; color: black; border-width: 2px; border-style: solid; padding: 4px; }"); QDesktopWidget *desktop = qApp->desktop(); mDesktopHeight = desktop->availableGeometry().height(); QSize curSize = SmGlobals::instance()->cursorSize(); setHoverOffset(QPoint(curSize.width() + 30, 0)); mMainLayout = new QHBoxLayout; mMainLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); mMainLayout->setContentsMargins(4, 4, 4, 4); mLabel = new QLabel; mMainLayout->addWidget(mLabel); setLayout(mMainLayout); } void HoverWindow::setContent(const QString &parent, const QStringList &children){ mAlignCenter = false; 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("
"); QTextDocument doc; doc.setHtml(curText); mLabel->setText(curText); setMinimumSize(doc.size().width() + 10, doc.size().height() + 10); setMaximumSize(doc.size().width() + 10, doc.size().height() + 10); raise(); } void HoverWindow::setPixmap(const QPixmap &pm, bool scale){ mAlignCenter = true; QPixmap curPm = pm; if(scale){ if(curPm.height() > 500){ curPm = curPm.scaledToHeight(500); } if(curPm.width() > 300){ curPm = curPm.scaledToWidth(300); } } mLabel->setPixmap(curPm); setMaximumSize(curPm.width() + 10, curPm.height() + 10); setMinimumSize(curPm.width() + 10, curPm.height() + 10); raise(); } 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); } void HoverWindow::setCaption(const QString &caption){ const QPixmap *pm = mLabel->pixmap(); if(!pm){ return; } QFontMetrics fm = qApp->fontMetrics(); QString elidedCap = fm.elidedText(caption, Qt::ElideLeft, pm->size().width() - 18, Qt::TextSingleLine); QStaticText myCap(elidedCap); QImage img = pm->toImage(); QPainter *p = new QPainter(&img); QColor bgColor(Qt::white); p->setBrush(QBrush(bgColor)); p->setPen(Qt::NoPen); p->setOpacity(0.4); qreal xStart = (mLabel->width() - myCap.size().width() - 9) / 2.0; p->drawRect(QRect(QPoint(xStart, 5), QSize(myCap.size().toSize()))); p->setOpacity(1.0); p->setPen(Qt::black); p->drawStaticText(xStart, 5, myCap); delete p; mLabel->setPixmap(QPixmap::fromImage(img)); } void HoverWindow::setPos(){ const QPoint globalPos(QCursor::pos()); QPoint hoverPos(globalPos.x() + mHoverOffset.x(), globalPos.y() + mHoverOffset.y()); if(mAlignCenter){ hoverPos = QPoint(hoverPos.x(), hoverPos.y() - height() / 2); } if(hoverPos.y() < 0){ hoverPos = QPoint(hoverPos.x(), 0); } if(hoverPos.y() + height() > mDesktopHeight){ hoverPos = QPoint(hoverPos.x(), mDesktopHeight - height()); } move(hoverPos); }