//=========================================== // Lumina-DE source code // Copyright (c) 2015, Ken Moore // Available under the 3-clause BSD license // See the LICENSE file for full details //=========================================== // This is a simple subclass for a QToolButton with black/white text (for transparent backgrounds) //=========================================== #ifndef _LUMINA_DESKTOP_PLUGIN_APPLAUNCHER_OUTLINE_TOOLBUTTON_H #define _LUMINA_DESKTOP_PLUGIN_APPLAUNCHER_OUTLINE_TOOLBUTTON_H #include #include #include #include #include #include #include #include #include class OutlineToolButton : public QToolButton{ Q_OBJECT public: OutlineToolButton(QWidget *parent=0) : QToolButton(parent){ //This button needs slightly different font settings - do this in the constructor so that other widgets can take it into account. QFont font = this->font(); font.setStyleStrategy(QFont::PreferAntialias); //Always set the font strategy (just in case it starts working down the road) font.setWeight(70); //need a slightly heavier weight due to outlining later this->setFont(font); } ~OutlineToolButton(){} protected: void paintEvent(QPaintEvent*){ /* NOTE: This is what a standard QToolButton performs (peeked at Qt source code for this tidbit) QStylePainter p(this); QStyleOptionToolButton opt; initStyleOption(&opt); p.drawComplexControl(QStyle::CC_ToolButton, opt); */ //Modify the standard QToolButton routine to paint the text differently QStylePainter p(this); QStyleOptionToolButton opt; initStyleOption(&opt); opt.font.setStyleStrategy(QFont::PreferAntialias); //Always set the font strategy (just in case it starts working down the road) opt.font.setWeight(65); //need a slightly heavier weight due to outlining later opt.text.clear(); //Don't paint the text yet - just the background/icon p.drawComplexControl(QStyle::CC_ToolButton, opt); //This does all the normal QToolButton stuff - just not text //Now get the text rectangle for the widget QRect box = p.style()->itemTextRect(opt.fontMetrics, opt.rect, Qt::AlignHCenter | Qt::AlignBottom, true, this->text()); //Get the QColors for the outline/text QColor textC = opt.palette.text().color().toHsl(); //need the lightness value in a moment QColor outC = textC; //qDebug() << "Font Color Values:" << textC << textC.lightness() << textC.lightnessF(); if(textC.lightnessF() > 0.5){ outC.setHsl(textC.hue(), textC.hslSaturation(), 0, 80); } else{outC.setHsl(textC.hue(), textC.hslSaturation(), 255, 80); } //qDebug() << "Outline Color Values:" << outC; //Now generate a QPainterPath for the text QPainterPath path; QStringList txt = this->text().split("\n"); //need each line independently, the newline actually gets painted otherwise for(int i=0; i