Commit 8b9d8a8c authored by kaniini's avatar kaniini

refactor icon utils out into their own file

parent 08cdaafc
<svg width="1536" height="1750" xmlns="http://www.w3.org/2000/svg">
<path d="M1024 896c0-141-115-256-256-256S512 755 512 896s115 256 256 256 256-115 256-256zm512-109v222c0 15-12 33-28 36l-185 28c-11 32-23 62-39 91 34 49 70 93 107 138 6 7 10 16 10 25s-3 16-9 23c-24 32-159 179-193 179-9 0-18-4-26-9l-138-108c-29 15-60 28-91 38-7 61-13 126-29 186-4 16-18 28-36 28H657c-18 0-34-13-36-30l-28-184c-31-10-61-22-90-37l-141 107c-7 6-16 9-25 9s-18-4-25-11c-53-48-123-110-165-168-5-7-7-15-7-23 0-9 3-16 8-23 34-46 71-90 105-137-17-32-31-65-41-99l-183-27c-17-3-29-19-29-36V783c0-15 12-33 27-36l186-28c10-32 23-62 39-92-34-48-70-93-107-138-6-7-10-15-10-24s4-16 9-23c24-33 159-179 193-179 9 0 18 4 26 10l138 107c29-15 60-28 91-38 7-61 13-126 29-186 4-16 18-28 36-28h222c18 0 34 13 36 30l28 184c31 10 61 22 90 37l142-107c6-6 15-9 24-9s18 4 25 10c53 49 123 111 165 170 5 6 7 14 7 22 0 9-3 16-8 23-34 46-71 90-105 137 17 32 31 65 41 98l183 28c17 3 29 19 29 36z"/>
</svg>
......@@ -37,7 +37,8 @@ SOURCES += \
ui/timelineselectorwidget.cc \
ui/timelinewidget.cc \
ui/notificationimpl.cc \
ui/threadwindow.cc
ui/threadwindow.cc \
ui/iconutils.cc
HEADERS += \
core/account.h \
......@@ -54,7 +55,8 @@ HEADERS += \
ui/timelineselectorwidget.h \
ui/timelinewidget.h \
ui/notificationimpl.h \
ui/threadwindow.h
ui/threadwindow.h \
ui/iconutils.h
FORMS +=
......
......@@ -9,5 +9,6 @@
<file>icons/action-repeat.svg</file>
<file>icons/action-favorite.svg</file>
<file>icons/action-visibility.svg</file>
<file>icons/action-preferences.svg</file>
</qresource>
</RCC>
#include "iconutils.h"
static QPixmap colorize_pixmap (QPixmap pixmap, QColor color) {
QPixmap result (pixmap.width (), pixmap.height ());
result.fill (color);
result.setMask (pixmap.createMaskFromColor (Qt::black, Qt::MaskOutColor));
return result;
}
static QColor desaturate (QColor c)
{
int h, s, v;
c.getHsv(&h, &s, &v);
s /= 2;
return QColor::fromHsv(h, s, v);
}
static QString icon_name_id (QString icon_name, QString fallback, IconState state){
return icon_name +" :" + fallback + state;
}
QPixmap pixmap_from_theme (QPalette palette, QString icon_name, QString fallback, IconState state, int size)
{
QIcon base = QIcon::fromTheme (icon_name, QIcon (fallback));
QIcon derived = QIcon ();
QString ico_key = icon_name_id (icon_name, fallback, state);
QPixmap cached_pixmap;
int supersize = size * 3;
if (! QPixmapCache::find (ico_key, &cached_pixmap))
{
switch (state)
{
case Default:
cached_pixmap = colorize_pixmap (base.pixmap (supersize, supersize),
palette.text ().color ());
break;
case Highlighted:
cached_pixmap = colorize_pixmap (base.pixmap (supersize, supersize),
desaturate (palette.highlight ().color ()));
break;
case Enabled:
cached_pixmap = colorize_pixmap (base.pixmap (supersize, supersize),
palette.highlight ().color ());
break;
}
QPixmapCache::insert (ico_key, cached_pixmap);
}
derived.addPixmap (cached_pixmap);
return derived.pixmap (size, size);
}
IconState proper_icon_state (QRect r, QPoint p, bool enabled)
{
// are we hovering over the icon?
if (r.contains (p))
return Highlighted;
// is the underlying feature enabled?
if (enabled)
return Enabled;
// return default
return Default;
}
#ifndef ICONUTILS_H
#define ICONUTILS_H
#include <QIcon>
#include <QImage>
#include <QPixmap>
#include <QPixmapCache>
#include <QBitmap>
#include <QPalette>
#include <QRect>
enum IconState {
Default,
Enabled,
Highlighted
};
#define ACTIONBAR_ICON_SIZE 16
#define ACTIONBAR_ICON_SUPERSIZED ACTIONBAR_ICON_SIZE * 3
QPixmap pixmap_from_theme (QPalette palette, QString icon_name, QString fallback, IconState state = Default, int size = ACTIONBAR_ICON_SIZE);
IconState proper_icon_state (QRect r, QPoint p, bool enabled = false);
#endif // ICONUTILS_H
......@@ -5,6 +5,7 @@
#include "mainwindow.h"
#include "onboardingwidget.h"
#include "composeform.h"
#include "iconutils.h"
MainWindow::MainWindow(QWidget *parent,
Michabo::AccountManager *manager)
......@@ -19,6 +20,7 @@ MainWindow::MainWindow(QWidget *parent,
m_toolbar(new QToolBar(m_main_container)),
m_compose_button(new QToolButton(m_toolbar)),
m_refresh_button(new QToolButton(m_toolbar)),
m_prefs_button(new QToolButton(m_toolbar)),
m_account_manager(manager)
{
// onboarding layout
......@@ -51,6 +53,19 @@ MainWindow::MainWindow(QWidget *parent,
m_toolbar->addWidget(m_refresh_button);
// prefs button
m_prefs_button->setIcon (pixmap_from_theme (palette (), "michabo-action-prefs",
":/embedded-assets/icons/action-preferences.svg",
Default, 24));
QObject::connect(m_prefs_button, &QToolButton::clicked, [=] () {
qDebug() << "Show preferences";
});
QWidget * spacer = new QWidget;
spacer->setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding);
m_toolbar->addWidget(spacer);
m_toolbar->addWidget(m_prefs_button);
// layout selection
QObject::connect(m_account_manager, &Michabo::AccountManager::accountAdded, [=] () {
updateLayout();
......
......@@ -33,6 +33,7 @@ private:
QToolBar *m_toolbar;
QToolButton *m_compose_button;
QToolButton *m_refresh_button;
QToolButton *m_prefs_button;
Michabo::AccountManager *m_account_manager;
void updateLayout();
......
......@@ -9,14 +9,13 @@
#include "timelinewidget.h"
#include "composeform.h"
#include "threadwindow.h"
#include "iconutils.h"
// control the size of avatars
#define AVATAR_ICON_SIZE 48
#define AVATAR_ICON_MARGIN 4
// control the size of the actionbar
#define ACTIONBAR_ICON_SIZE 16
#define ACTIONBAR_ICON_SUPERSIZED ACTIONBAR_ICON_SIZE * 3
#define ACTIONBAR_CONTAINER_MARGIN 8
#define ACTIONBAR_CONTAINER_HEIGHT (ACTIONBAR_ICON_SIZE + (ACTIONBAR_CONTAINER_MARGIN * 2))
#define ACTIONBAR_ICON_SPACING 20
......@@ -64,77 +63,6 @@ QSize AvatarDelegate::sizeHint(const QStyleOptionViewItem &, const QModelIndex &
return QSize(AVATAR_ICON_SIZE, AVATAR_ICON_SIZE);
}
enum IconState {
Default,
Enabled,
Highlighted
};
static QPixmap colorize_pixmap (QPixmap pixmap, QColor color) {
QPixmap result (pixmap.width (), pixmap.height ());
result.fill (color);
result.setMask (pixmap.createMaskFromColor (Qt::black, Qt::MaskOutColor));
return result;
}
static QColor desaturate (QColor c)
{
int h, s, v;
c.getHsv(&h, &s, &v);
s /= 2;
return QColor::fromHsv(h, s, v);
}
static QString icon_name_id (QString icon_name, QString fallback, IconState state){
return icon_name +" :" + fallback + state;
}
static QPixmap pixmap_from_theme (QPalette palette, QString icon_name, QString fallback, IconState state=Default)
{
QIcon base = QIcon::fromTheme (icon_name, QIcon (fallback));
QIcon derived = QIcon ();
QString ico_key=icon_name_id (icon_name, fallback, state);
QPixmap cached_pixmap;
if(!QPixmapCache::find (ico_key, &cached_pixmap)){
switch(state){
case Default:
cached_pixmap = colorize_pixmap (base.pixmap (ACTIONBAR_ICON_SUPERSIZED, ACTIONBAR_ICON_SUPERSIZED),
palette.text ().color ());
break;
case Highlighted:
cached_pixmap = colorize_pixmap (base.pixmap (ACTIONBAR_ICON_SUPERSIZED, ACTIONBAR_ICON_SUPERSIZED),
desaturate (palette.highlight ().color ()));
break;
case Enabled:
cached_pixmap = colorize_pixmap (base.pixmap (ACTIONBAR_ICON_SUPERSIZED, ACTIONBAR_ICON_SUPERSIZED),
palette.highlight ().color ());
break;
}
QPixmapCache::insert (ico_key, cached_pixmap);
}
derived.addPixmap (cached_pixmap);
return derived.pixmap (ACTIONBAR_ICON_SIZE, ACTIONBAR_ICON_SIZE);
}
static IconState proper_icon_state(QRect r, QPoint p, bool enabled = false)
{
// are we hovering over the icon?
if (r.contains (p))
return Highlighted;
// is the underlying feature enabled?
if (enabled)
return Enabled;
// return default
return Default;
}
// the geometry_cb is used to set up geometry and clipping
// the rect_cb is to used to actually draw or check hitboxes
QSize HTMLDelegate::doGridLayout(const QStyleOptionViewItem &option,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment