Qt: clean up gui_settings.h - move general functions to qt_utils.h

This commit is contained in:
Megamouse 2018-03-17 00:13:40 +01:00 committed by Ivan
parent c10e195dba
commit baea538c32
15 changed files with 187 additions and 175 deletions

View file

@ -1,6 +1,8 @@
#include "qt_utils.h"
#include <QApplication>
#include <QBitmap>
#include <QPainter>
#include <QScreen>
namespace gui
@ -33,5 +35,125 @@ namespace gui
return QRect(frame_x, frame_y, width, height);
}
QPixmap get_colorized_pixmap(const QPixmap& old_pixmap, const QColor& old_color, const QColor& new_color, bool use_special_masks, bool colorize_all)
{
QPixmap pixmap = old_pixmap;
if (colorize_all)
{
QBitmap mask = pixmap.createMaskFromColor(Qt::transparent, Qt::MaskInColor);
pixmap.fill(new_color);
pixmap.setMask(mask);
return pixmap;
}
QBitmap mask = pixmap.createMaskFromColor(old_color, Qt::MaskOutColor);
pixmap.fill(new_color);
pixmap.setMask(mask);
// special masks for disc icon and others
if (use_special_masks)
{
// Example usage for an icon with multiple shades of the same color
//auto saturatedColor = [](const QColor& col, float sat /* must be < 1 */)
//{
// int r = col.red() + sat * (255 - col.red());
// int g = col.green() + sat * (255 - col.green());
// int b = col.blue() + sat * (255 - col.blue());
// return QColor(r, g, b, col.alpha());
//};
//QColor test_color(0, 173, 246, 255);
//QPixmap test_pixmap = old_pixmap;
//QBitmap test_mask = test_pixmap.createMaskFromColor(test_color, Qt::MaskOutColor);
//test_pixmap.fill(saturatedColor(new_color, 0.6f));
//test_pixmap.setMask(test_mask);
QColor white_color(Qt::white);
QPixmap white_pixmap = old_pixmap;
QBitmap white_mask = white_pixmap.createMaskFromColor(white_color, Qt::MaskOutColor);
white_pixmap.fill(white_color);
white_pixmap.setMask(white_mask);
QPainter painter(&pixmap);
painter.drawPixmap(QPoint(0, 0), white_pixmap);
//painter.drawPixmap(QPoint(0, 0), test_pixmap);
painter.end();
}
return pixmap;
}
QIcon get_colorized_icon(const QIcon& old_icon, const QColor& old_color, const QColor& new_color, bool use_special_masks, bool colorize_all)
{
return QIcon(get_colorized_pixmap(old_icon.pixmap(old_icon.availableSizes().at(0)), old_color, new_color, use_special_masks, colorize_all));
}
QStringList get_dir_entries(const QDir& dir, const QStringList& name_filters)
{
QFileInfoList entries = dir.entryInfoList(name_filters, QDir::Files);
QStringList res;
for (const QFileInfo &entry : entries)
{
res.append(entry.baseName());
}
return res;
}
QColor get_label_color(const QString& object_name, QPalette::ColorRole color_role)
{
QLabel dummy_color;
dummy_color.setObjectName(object_name);
dummy_color.ensurePolished();
return dummy_color.palette().color(color_role);
};
QFont get_label_font(const QString& object_name)
{
QLabel dummy_font;
dummy_font.setObjectName(object_name);
dummy_font.ensurePolished();
return dummy_font.font();
};
QImage get_opaque_image_area(const QString& path)
{
QImage image = QImage(path);
int w_min = 0;
int w_max = image.width();
int h_min = 0;
int h_max = image.height();
for (int y = 0; y < image.height(); ++y)
{
QRgb *row = (QRgb*)image.scanLine(y);
bool row_filled = false;
for (int x = 0; x < image.width(); ++x)
{
if (qAlpha(row[x]))
{
row_filled = true;
w_min = std::max(w_min, x);
if (w_max > x)
{
w_max = x;
x = w_min;
}
}
}
if (row_filled)
{
h_max = std::min(h_max, y);
h_min = y;
}
}
return image.copy(QRect(QPoint(w_max, h_max), QPoint(w_min, h_min)));
}
} // utils
} // gui