Make code compatible with QT 5.9 (#1553)

* Make code compatible with QT 5.9, Ubuntu 18.04.

* Fix high dpi scaling in ColorThemeListView.
This commit is contained in:
karliss 2019-05-19 13:24:24 +03:00 committed by Itay Cohen
parent 051eb3aaf3
commit 1aabddc3f6
6 changed files with 38 additions and 23 deletions

View File

@ -1,7 +1,9 @@
cmake_minimum_required(VERSION 3.1) cmake_minimum_required(VERSION 3.1)
cmake_policy(SET CMP0074 NEW) if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12.0")
cmake_policy(SET CMP0074 NEW)
endif()
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(DisallowInSource) include(DisallowInSource)

View File

@ -61,8 +61,8 @@ int main(int argc, char *argv[])
continue; continue;
} }
QJsonObject updatedTheme; QJsonObject updatedTheme;
auto sch = ThemeWorker().getTheme(theme); auto sch = ThemeWorker().getTheme(theme).object();
for (auto key : sch.object().keys()) { for (auto key : sch.keys()) {
if (options.contains(key)) { if (options.contains(key)) {
updatedTheme.insert(key, sch[key]); updatedTheme.insert(key, sch[key]);
} }

View File

@ -22,7 +22,10 @@ ColorThemeEditDialog::ColorThemeEditDialog(QWidget *parent) :
previewDisasmWidget = new DisassemblyWidget(nullptr); previewDisasmWidget = new DisassemblyWidget(nullptr);
previewDisasmWidget->setObjectName("Preview Disasm"); previewDisasmWidget->setObjectName("Preview Disasm");
previewDisasmWidget->setPreviewMode(true); previewDisasmWidget->setPreviewMode(true);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
// default size limit is acceptable
previewDisasmWidget->setMinimumSize(qApp->screenAt(previewDisasmWidget->pos())->size() * 0.5); previewDisasmWidget->setMinimumSize(qApp->screenAt(previewDisasmWidget->pos())->size() * 0.5);
#endif
previewDisasmWidget->setWindowTitle(tr("Disassembly Preview")); previewDisasmWidget->setWindowTitle(tr("Disassembly Preview"));
previewDisasmWidget->setFeatures(QDockWidget::NoDockWidgetFeatures); previewDisasmWidget->setFeatures(QDockWidget::NoDockWidgetFeatures);
ui->colorPickerAndPreviewLayout->addWidget(previewDisasmWidget); ui->colorPickerAndPreviewLayout->addWidget(previewDisasmWidget);

View File

@ -194,8 +194,7 @@ ColorPicker::ColorPicker(QWidget* parent) :
ColorPicker::~ColorPicker() ColorPicker::~ColorPicker()
{ {
if (pickingFromScreen) { if (pickingFromScreen) {
setColor(QApplication::screenAt(QCursor::pos())->grabWindow(QApplication::desktop()->winId()) setColor(getColorAtMouse());
.toImage().pixelColor(QCursor::pos()));
stopPickingFromScreen(); stopPickingFromScreen();
} }
} }
@ -282,11 +281,7 @@ void ColorPicker::startPickingFromScreen()
void ColorPicker::mouseReleaseEvent(QMouseEvent* event) void ColorPicker::mouseReleaseEvent(QMouseEvent* event)
{ {
if (pickingFromScreen) { if (pickingFromScreen) {
const QDesktopWidget *desktop = QApplication::desktop(); setColor(getColorAtMouse());
const QPixmap pixmap = QGuiApplication::screens().at(desktop->screenNumber())
->grabWindow(desktop->winId(),
QCursor::pos().x(), QCursor::pos().y(), 1, 1);
setColor(pixmap.toImage().pixel(0, 0));
pickingFromScreen = false; pickingFromScreen = false;
setMouseTracking(false); setMouseTracking(false);
releaseMouse(); releaseMouse();
@ -297,13 +292,18 @@ void ColorPicker::mouseReleaseEvent(QMouseEvent* event)
void ColorPicker::mouseMoveEvent(QMouseEvent* event) void ColorPicker::mouseMoveEvent(QMouseEvent* event)
{ {
if (pickingFromScreen) { if (pickingFromScreen) {
updateColor(getColorAtMouse());
}
QWidget::mouseMoveEvent(event);
}
QColor ColorPicker::getColorAtMouse()
{
const QDesktopWidget *desktop = QApplication::desktop(); const QDesktopWidget *desktop = QApplication::desktop();
const QPixmap pixmap = QGuiApplication::screens().at(desktop->screenNumber()) const QPixmap pixmap = QGuiApplication::screens().at(desktop->screenNumber())
->grabWindow(desktop->winId(), ->grabWindow(desktop->winId(),
QCursor::pos().x(), QCursor::pos().y(), 1, 1); QCursor::pos().x(), QCursor::pos().y(), 1, 1);
updateColor(pixmap.toImage().pixel(0, 0)); return pixmap.toImage().pixelColor(0, 0);
}
QWidget::mouseMoveEvent(event);
} }
bool ColorPicker::isPickingFromScreen() const bool ColorPicker::isPickingFromScreen() const

View File

@ -79,6 +79,8 @@ private:
Ui::ColorPicker *ui; Ui::ColorPicker *ui;
bool pickingFromScreen; bool pickingFromScreen;
QColor getColorAtMouse();
/** /**
* @brief bufferColor is used to buffer current color while picking from screen. * @brief bufferColor is used to buffer current color while picking from screen.
*/ */

View File

@ -37,7 +37,7 @@ void ColorOptionDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option, const QStyleOptionViewItem &option,
const QModelIndex &index) const const QModelIndex &index) const
{ {
int margin = this->margin * qApp->screenAt(option.rect.topLeft())->devicePixelRatio(); int margin = this->margin * painter->device()->devicePixelRatioF();
painter->save(); painter->save();
painter->setFont(option.font); painter->setFont(option.font);
painter->setRenderHint(QPainter::Antialiasing); painter->setRenderHint(QPainter::Antialiasing);
@ -88,10 +88,16 @@ void ColorOptionDelegate::paint(QPainter *painter,
resetButtonRect.setSize(resetButtonRect.size() * 1.0); resetButtonRect.setSize(resetButtonRect.size() * 1.0);
} }
} else { } else {
QColor c = qApp->palette().placeholderText().color(); #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
QColor placeholderColor = qApp->palette().placeholderText().color();
#else
QColor placeholderColor = qApp->palette().text().color();
placeholderColor.setAlphaF(0.5);
#endif
QColor c = placeholderColor;
c.setAlphaF(0.2); c.setAlphaF(0.2);
br = c; br = c;
pen = QPen(qApp->palette().placeholderText().color().darker(), margin / 2); pen = QPen(placeholderColor.darker(), margin / 2);
} }
painter->fillRect(option.rect, br); painter->fillRect(option.rect, br);
@ -143,9 +149,9 @@ void ColorOptionDelegate::paint(QPainter *painter,
QSize ColorOptionDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const QSize ColorOptionDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{ {
int margin = this->margin * qApp->screenAt(option.rect.topLeft())->devicePixelRatio(); qreal margin = this->margin * option.widget->devicePixelRatioF();
int fontHeight = option.fontMetrics.height(); qreal fontHeight = option.fontMetrics.height();
int h = QPen().width(); qreal h = QPen().width();
h += fontHeight; // option name h += fontHeight; // option name
h += margin / 2; // margin between option rect and option name h += margin / 2; // margin between option rect and option name
h += margin / 4; // margin betveen option rect and color rect h += margin / 4; // margin betveen option rect and color rect
@ -154,7 +160,7 @@ QSize ColorOptionDelegate::sizeHint(const QStyleOptionViewItem& option, const QM
h += margin; // last margin h += margin; // last margin
Q_UNUSED(index) Q_UNUSED(index)
return QSize(-1, h); return QSize(-1, qRound(h));
} }
QRect ColorOptionDelegate::getResetButtonRect() const QRect ColorOptionDelegate::getResetButtonRect() const
@ -187,6 +193,7 @@ ColorThemeListView::ColorThemeListView(QWidget *parent) :
setModel(new ColorSettingsModel(static_cast<QObject *>(this))); setModel(new ColorSettingsModel(static_cast<QObject *>(this)));
static_cast<ColorSettingsModel *>(this->model())->updateTheme(); static_cast<ColorSettingsModel *>(this->model())->updateTheme();
setItemDelegate(new ColorOptionDelegate(this)); setItemDelegate(new ColorOptionDelegate(this));
setResizeMode(ResizeMode::Adjust);
QJsonArray rgb = qobject_cast<ColorSettingsModel*>(model())->getTheme() QJsonArray rgb = qobject_cast<ColorSettingsModel*>(model())->getTheme()
.object().find("gui.background").value().toArray(); .object().find("gui.background").value().toArray();
@ -235,7 +242,8 @@ void ColorThemeListView::mouseReleaseEvent(QMouseEvent* e)
auto model = qobject_cast<ColorSettingsModel*>(this->model()); auto model = qobject_cast<ColorSettingsModel*>(this->model());
ColorOption co = currentIndex().data(Qt::UserRole).value<ColorOption>(); ColorOption co = currentIndex().data(Qt::UserRole).value<ColorOption>();
co.changed = false; co.changed = false;
QJsonArray rgb = ThemeWorker().getTheme(Config()->getColorTheme())[co.optionName].toArray(); QJsonArray rgb = ThemeWorker().getTheme(
Config()->getColorTheme()).object()[co.optionName].toArray();
co.color = QColor(rgb[0].toInt(), rgb[1].toInt(), rgb[2].toInt()); co.color = QColor(rgb[0].toInt(), rgb[1].toInt(), rgb[2].toInt());
model->setData(currentIndex(), QVariant::fromValue(co)); model->setData(currentIndex(), QVariant::fromValue(co));
QCursor c; QCursor c;