Fixed the issue of dark tree expansion icons

Prior to this bug fix, the expansion icons were
not visible with a dark background on some platforms
(i.e. Gtk style, native Windows style).
This commit is contained in:
Matthias Koefferlein 2017-03-02 00:26:16 +01:00
parent bf15e46c10
commit 29727f597d
6 changed files with 108 additions and 9 deletions

View File

@ -35,6 +35,9 @@
#include <QLabel>
#include <QToolButton>
#include <QCheckBox>
#include <QProxyStyle>
#include <QPainter>
#include <QPen>
#include "dbClipboard.h"
#include "dbClipboardData.h"
@ -321,6 +324,8 @@ HierarchyControlPanel::HierarchyControlPanel (lay::LayoutView *view, QWidget *pa
mp_view->cellviews_changed_event.add (this, &HierarchyControlPanel::update_required);
mp_view->hier_changed_event.add (this, &HierarchyControlPanel::update_required);
mp_tree_style.reset (new BackgroundAwareTreeStyle (style ()));
do_update_content ();
}
@ -660,13 +665,6 @@ void
HierarchyControlPanel::set_background_color (QColor c)
{
m_background_color = c;
QColor hl;
if (c.green () > 128) {
hl = QColor (192, 192, 255);
} else {
hl = QColor (0, 0, 80);
}
for (std::vector <QTreeView *>::const_iterator f = mp_cell_lists.begin (); f != mp_cell_lists.end (); ++f) {
QPalette pl ((*f)->palette ());
pl.setColor (QPalette::Base, c);
@ -889,6 +887,7 @@ HierarchyControlPanel::do_update_content (int cv_index)
HCPCellTreeWidget *cell_list = new HCPCellTreeWidget (cl_frame, "tree");
cl_ly->addWidget (cell_list);
cell_list->setStyle (mp_tree_style.get ());
cell_list->setModel (new CellTreeModel (cell_list, mp_view, cv_index, m_flat ? CellTreeModel::Flat : 0, 0, m_sorting));
cell_list->setUniformRowHeights (true);

View File

@ -300,6 +300,7 @@ private:
QColor m_text_color;
tl::DeferredMethod<HierarchyControlPanel> m_do_update_content_dm;
tl::DeferredMethod<HierarchyControlPanel> m_do_full_update_content_dm;
std::auto_ptr<QStyle> mp_tree_style;
// locate the CellTreeItem in the tree corresponding to a partial path starting from p.
CellTreeItem *find_child_item (cell_path_type::const_iterator start, cell_path_type::const_iterator end, CellTreeItem *p);

View File

@ -308,6 +308,8 @@ LayerControlPanel::LayerControlPanel (lay::LayoutView *view, db::Manager *manage
mp_model = new lay::LayerTreeModel (this, view);
mp_layer_list = new LCPTreeWidget (this, mp_model, "layer_tree");
mp_ll_style.reset (new BackgroundAwareTreeStyle (mp_layer_list->style ()));
mp_layer_list->setStyle (mp_ll_style.get ());
mp_model->set_font (mp_layer_list->font ());
/*
* At least with Qt 4.2.x setting uniform row heights has a strange side effect:

View File

@ -326,6 +326,7 @@ public slots:
private:
QTabBar *mp_tab_bar;
LCPTreeWidget *mp_layer_list;
std::auto_ptr<QStyle> mp_ll_style;
LayerTreeModel *mp_model;
lay::LayoutView *mp_view;
bool m_needs_update;

View File

@ -934,8 +934,9 @@ const int le_decoration_space = 2; // additional distance between decoration ic
DecoratedLineEdit::DecoratedLineEdit (QWidget *parent)
: QLineEdit (parent),
m_clear_button_enabled (false), m_options_button_enabled (false), mp_options_menu (0),
m_escape_signal_enabled (false), m_tab_signal_enabled (false)
m_clear_button_enabled (false), m_options_button_enabled (false),
m_escape_signal_enabled (false), m_tab_signal_enabled (false),
mp_options_menu (0)
{
mp_options_label = new QLabel (this);
mp_options_label->hide ();
@ -1105,5 +1106,84 @@ void DecoratedLineEdit::resizeEvent (QResizeEvent * /*event*/)
}
}
// -------------------------------------------------------------
// BackgroundAwareTreeStyle implementation
BackgroundAwareTreeStyle::BackgroundAwareTreeStyle (QStyle *org_style)
: QProxyStyle (org_style)
{
// .. nothing yet ..
}
void
BackgroundAwareTreeStyle::drawPrimitive (QStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const
{
if (pe == PE_IndicatorBranch) {
static const int sz = 9;
int mid_h = opt->rect.x () + opt->rect.width () / 2;
int mid_v = opt->rect.y () + opt->rect.height () / 2;
if (opt->state & State_Children) {
QColor c;
QPalette::ColorGroup cg = QPalette::Disabled;
if ((w && w->isEnabled ()) || (!w && (opt->state & State_Enabled))) {
if ((w && w->hasFocus ()) || (!w && (opt->state & State_HasFocus))) {
cg = QPalette::Normal;
} else {
cg = QPalette::Inactive;
}
}
if (opt->state & State_Selected) {
c = opt->palette.color (cg, QPalette::HighlightedText);
} else {
c = opt->palette.color (cg, QPalette::Text);
}
if (! (opt->state & State_MouseOver)) {
if (c.green () < 128) {
c = QColor ((c.red () * 3 + 255) / 4, (c.green () * 3 + 255) / 4, (c.blue () * 3 + 255) / 4);
} else {
c = QColor ((c.red () * 8) / 9, (c.green () * 8) / 9, (c.blue () * 8) / 9);
}
}
QPen old_pen = p->pen ();
p->setPen (Qt::NoPen);
QBrush old_brush = p->brush ();
p->setBrush (c);
QPainter::RenderHints old_rh = p->renderHints ();
p->setRenderHints (QPainter::Antialiasing);
if (opt->state & State_Open) {
QPoint points[] = {
QPoint (mid_h - sz / 2, mid_v - sz / 3),
QPoint (mid_h + sz / 2, mid_v - sz / 3),
QPoint (mid_h, mid_v + sz / 3)
};
p->drawPolygon (points, sizeof (points) / sizeof (points[0]));
} else {
QPoint points[] = {
QPoint (mid_h - sz / 3, mid_v - sz / 2),
QPoint (mid_h + sz / 3, mid_v),
QPoint (mid_h - sz / 3, mid_v + sz / 2)
};
p->drawPolygon (points, sizeof (points) / sizeof (points[0]));
}
p->setPen (old_pen);
p->setBrush (old_brush);
p->setRenderHints (old_rh);
return;
}
}
QProxyStyle::drawPrimitive (pe, opt, p, w);
}
}

View File

@ -30,6 +30,7 @@
#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QProxyStyle>
namespace db
{
@ -443,6 +444,21 @@ private:
int m_default_left_margin, m_default_right_margin;
};
/**
* @brief A style tailoring the drawing of the branch indicator
* This proxy style is making the branch indicator a triangle and aware of the
* palette of the tree.
* The default Gtk style is not, hence making the background dark means the
* triangles become invisible.
*/
class BackgroundAwareTreeStyle
: public QProxyStyle
{
public:
BackgroundAwareTreeStyle (QStyle *org_style);
void drawPrimitive (PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const;
};
} // namespace lay
#endif