Skip to content

Commit

Permalink
combobox lists appearance improved
Browse files Browse the repository at this point in the history
  • Loading branch information
aliakseis committed Mar 8, 2019
1 parent d3b5ecd commit 863c1c2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
26 changes: 4 additions & 22 deletions resources/LIII/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,14 @@ QComboBox
border-radius: 4px;
background-color: white;
margin-right: 0px;
margin-top: 3px;
margin-bottom: 3px;
}

QComboBox:on { /* shift the text when the popup opens */
border-bottom-left-radius: 0px;
padding-bottom: 3px;
margin-bottom: 0px;
}

QComboBox::drop-down
Expand Down Expand Up @@ -177,32 +181,10 @@ QComboBox::down-arrow:on
image: url(:/combobox-down-arrow-press.png);
}


QComboBox QAbstractItemView
{
border: 1px solid #cfcfcf;
border-top: none;
selection-background-color: #d1f3fc;
selection-color: black;
}

TorrentDetailsForm QComboBox
{
margin-top: 3px;
margin-bottom: 3px;
}

TorrentDetailsForm QComboBox:on { /* shift the text when the popup opens */
padding-bottom: 3px;
margin-bottom: 0px;
}

AddLinks QComboBox
{
margin-top: 3px;
margin-bottom: 3px;
}
AddLinks QComboBox:on { /* shift the text when the popup opens */
padding-bottom: 3px;
margin-bottom: 0px;
}
55 changes: 55 additions & 0 deletions src/gui/LIIIstyle.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "LIIIstyle.h"
#include <QStyleOption>
#include <QPainter>
#include <QComboBox>
#include <QAbstractItemView>

#ifdef Q_OS_WIN
enum { ROUND_RADIUS = 3, FONT_SIZE = 8 };
Expand Down Expand Up @@ -113,3 +115,56 @@ void LIIIStyle::drawPrimitive(PrimitiveElement element, const QStyleOption* opti
QProxyStyle::drawPrimitive(element, option, painter, widget);
}
}

// https://stackoverflow.com/questions/20554940/qcombobox-pop-up-expanding-and-qtwebkit/20909625#20909625
int LIIIStyle::styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
{
if (hint == QStyle::SH_ComboBox_Popup)
{
auto combo = static_cast<const QComboBox *>(widget);
QAbstractItemView* view = nullptr;
for (auto child : combo->children())
{
for (auto v : child->children())
{
view = qobject_cast<QAbstractItemView*>(v);
if (view)
{
break;
}
}

if (view)
{
break;
}
}

if (view)
{
const int iconSize = combo->iconSize().width();
const QFontMetrics fontMetrics1 = view->fontMetrics();
const QFontMetrics fontMetrics2 = combo->fontMetrics();
const int j = combo->count();
int width = combo->width(); //default width

for (int i = 0; i < j; ++i) {
const int textWidth = qMax(
fontMetrics1.width(combo->itemText(i) + "WW"),
fontMetrics2.width(combo->itemText(i) + "WW")
);

if (combo->itemIcon(i).isNull()) {
width = qMax(width, textWidth);
}
else {
width = qMax(width, textWidth + iconSize);
}
}

view->setFixedWidth(width);
}
}

return QProxyStyle::styleHint(hint, option, widget, returnData);
}
1 change: 1 addition & 0 deletions src/gui/LIIIstyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ class LIIIStyle
public:
void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const override;
void drawPrimitive(PrimitiveElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const override;
int styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const override;
};

0 comments on commit 863c1c2

Please sign in to comment.