From 81e6526005d7c31e2e5f2d0554dabe1e863fb8be Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 7 Dec 2020 16:19:30 +0200 Subject: Use custom icons in webviewcontextmenu --- src/CMakeLists.txt | 5 +- src/autogen/CMakeLists.txt | 8 ++- src/autogen/util.cpp | 47 ++++++++++++++++ src/autogen/util.h | 103 +++++++++++++++++++++++++++++++++++ src/session/sessiondialog.cpp | 2 +- src/util.cpp | 48 ---------------- src/util.h | 95 -------------------------------- src/webengine/CMakeLists.txt | 2 +- src/webengine/webviewcontextmenu.cpp | 29 ++++++---- 9 files changed, 177 insertions(+), 162 deletions(-) create mode 100644 src/autogen/util.cpp create mode 100644 src/autogen/util.h delete mode 100644 src/util.cpp delete mode 100644 src/util.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8754e54..5139d7c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,12 +31,11 @@ add_executable(poi main.cpp applicationmenu.h applicationmenu.cpp - browser.h browser.cpp - util.h util.cpp) + browser.h browser.cpp) target_link_libraries(poi PRIVATE Qt5::Widgets Qt5::WebEngineWidgets Qt5::Svg SingleApplication::SingleApplication fmt spdlog - about_dialog about_plugin webengine + autogen about_dialog about_plugin webengine bookmarks configuration downloads pluginloader session_formats smolblok) target_include_directories(poi PRIVATE . ${CMAKE_SOURCE_DIR}/include diff --git a/src/autogen/CMakeLists.txt b/src/autogen/CMakeLists.txt index 66065ee..c5c7614 100644 --- a/src/autogen/CMakeLists.txt +++ b/src/autogen/CMakeLists.txt @@ -15,7 +15,9 @@ add_custom_command(OUTPUT poi_logos.h DEPENDS ${poi_logos} COMMAND ${PYTHON} ${RCC} -o=poi_logos.h dump -ns=logos ${poi_logos}) # poi_icons.h -set(poi_icons icons/arrow-left.svg icons/arrow-right.svg icons/circle-x.svg icons/refresh.svg icons/home.svg) +set(poi_icons + icons/arrow-left.svg icons/arrow-right.svg icons/circle-x.svg icons/refresh.svg icons/home.svg + icons/volume.svg icons/volume-3.svg) foreach(f ${poi_icons}) list(REMOVE_ITEM poi_icons ${f}) list(APPEND poi_icons ${ICONS_PATH}/${f}) @@ -27,5 +29,7 @@ add_custom_command(OUTPUT poi_icons.h DEPENDS ${poi_icons} add_custom_target(py_autogen DEPENDS settings.h poi_logos.h poi_icons.h) add_library(autogen INTERFACE) -target_include_directories(autogen INTERFACE ${CMAKE_CURRENT_BINARY_DIR}) +target_sources(autogen INTERFACE util.h util.cpp) +target_include_directories(autogen INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(autogen INTERFACE Qt5::Svg) add_dependencies(autogen py_autogen) diff --git a/src/autogen/util.cpp b/src/autogen/util.cpp new file mode 100644 index 0000000..0120be2 --- /dev/null +++ b/src/autogen/util.cpp @@ -0,0 +1,47 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "util.h" +#include +#include +#include + +#define ListSeparator QLatin1String(";") + +const QStringList Util::files(const QString &location, const QStringList &nameFilters) +{ + if(location.isEmpty()) return QStringList(); + + QStringList filelist; + + // check if location is a list of locations (contains a ';') + if(location.contains(ListSeparator)) { + const QStringList locations = location.split(ListSeparator); + + for(const QString &l : locations) { + filelist.append(Util::files(l, nameFilters)); + } + + return filelist; + } + + const QFileInfo info(location); + + // check if location is a folder + if(info.isDir()) { + const auto entryList = QDir(info.absoluteFilePath()).entryInfoList(nameFilters, QDir::Files | QDir::Readable, QDir::Time); + for(const QFileInfo &entryInfo : entryList) { + filelist.append(entryInfo.absoluteFilePath()); + } + } else if(info.isFile()) { + filelist.append(info.absoluteFilePath()); + } + + return filelist; +} + diff --git a/src/autogen/util.h b/src/autogen/util.h new file mode 100644 index 0000000..822794a --- /dev/null +++ b/src/autogen/util.h @@ -0,0 +1,103 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_UTIL_H +#define SMOLBOTE_UTIL_H + +#include "poi_icons.h" +#include "poi_logos.h" +#include +#include +#include +#include +#include + +namespace Util { + +const QStringList files(const QString &location, const QStringList &nameFilters = QStringList()); + +constexpr auto AppIcon = QStyle::SP_CustomBase + 1; +constexpr auto AppWindowIcon = QStyle::SP_CustomBase + 2; + +[[nodiscard]] inline QPixmap render(const auto &data, const QSize size = QSize()) +{ + QSvgRenderer renderer; + renderer.load(QByteArray(data.data(), static_cast(data.size()))); + + QPixmap pm(size.isValid() ? size : renderer.defaultSize()); + pm.fill(Qt::transparent); + + QPainter painter(&pm); + renderer.render(&painter, pm.rect()); + + return pm; +} + +template +inline QIcon icon() +{ + using namespace std::literals; + + if constexpr(id == QStyle::SP_MediaVolume) { + auto on = render(icons::get([] { return "volume-3.svg"sv; })); + auto off = render(icons::get([] { return "volume.svg"sv; })); + QIcon m; + m.addPixmap(on, QIcon::Normal, QIcon::On); + m.addPixmap(off, QIcon::Normal, QIcon::Off); + return m; + + } else if constexpr(id >= QStyle::SP_CustomBase) { + constexpr auto data = logos::get([] { + switch(id) { + case AppIcon: + return "poi.svg"sv; + case AppWindowIcon: + return "poi_window.svg"sv; + default: + return "__unknown__"sv; + } + }); + return QIcon(render(data)); + } else { + + constexpr auto data = icons::get([] { + switch(id) { + case QStyle::SP_ArrowBack: + return "arrow-left.svg"sv; + case QStyle::SP_ArrowForward: + return "arrow-right.svg"sv; + case QStyle::SP_BrowserStop: + return "circle-x.svg"sv; + case QStyle::SP_BrowserReload: + return "refresh.svg"sv; + case QStyle::SP_DirHomeIcon: + return "home.svg"sv; + default: + return "__unknown__"sv; + } + }); + + QIcon m; + // This is a horrible hack that will one day be hopefully fixed: + // When rendering an svg, you can't actually pick a stroke color through + // QPainter::setBrush or QPainter::setPen. So instead, replace the stroke + // color in the data (stroke="currentColor") and then rerender it. + + QByteArray arr(data.data(), data.size()); + for(const auto mode : { QIcon::Normal /*, QIcon::Disabled, QIcon::Active, QIcon::Selected*/ }) { + for(const auto toggle : { QIcon::On, QIcon::Off }) { + const auto pm = render(arr.replace("currentColor", (toggle == QIcon::On ? "black" : "gray"))); + m.addPixmap(pm, mode, toggle); + } + } + return m; + } +} +} // namespace Util + +#endif // SMOLBOTE_UTIL_H diff --git a/src/session/sessiondialog.cpp b/src/session/sessiondialog.cpp index 36cf9cb..548f73e 100644 --- a/src/session/sessiondialog.cpp +++ b/src/session/sessiondialog.cpp @@ -8,7 +8,7 @@ #include "sessiondialog.h" #include "../browser.h" -#include "../util.h" +#include "util.h" #include "configuration.h" #include "session_json.hpp" #include "ui_sessiondialog.h" diff --git a/src/util.cpp b/src/util.cpp deleted file mode 100644 index f18b682..0000000 --- a/src/util.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "util.h" -#include -#include -#include -#include - -#define ListSeparator QLatin1String(";") - -const QStringList Util::files(const QString &location, const QStringList &nameFilters) -{ - if(location.isEmpty()) return QStringList(); - - QStringList filelist; - - // check if location is a list of locations (contains a ';') - if(location.contains(ListSeparator)) { - const QStringList locations = location.split(ListSeparator); - - for(const QString &l : locations) { - filelist.append(Util::files(l, nameFilters)); - } - - return filelist; - } - - const QFileInfo info(location); - - // check if location is a folder - if(info.isDir()) { - const auto entryList = QDir(info.absoluteFilePath()).entryInfoList(nameFilters, QDir::Files | QDir::Readable, QDir::Time); - for(const QFileInfo &entryInfo : entryList) { - filelist.append(entryInfo.absoluteFilePath()); - } - } else if(info.isFile()) { - filelist.append(info.absoluteFilePath()); - } - - return filelist; -} - diff --git a/src/util.h b/src/util.h deleted file mode 100644 index 8431a5b..0000000 --- a/src/util.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_UTIL_H -#define SMOLBOTE_UTIL_H - -#include "poi_icons.h" -#include "poi_logos.h" -#include -#include -#include -#include -#include - -namespace Util { - -const QStringList files(const QString &location, const QStringList &nameFilters = QStringList()); - -constexpr auto AppIcon = QStyle::SP_CustomBase + 1; -constexpr auto AppWindowIcon = QStyle::SP_CustomBase + 2; - -[[nodiscard]] inline QPixmap render(const auto &data, const QSize size = QSize()) -{ - QSvgRenderer renderer; - renderer.load(QByteArray(data.data(), static_cast(data.size()))); - - QPixmap pm(size.isValid() ? size : renderer.defaultSize()); - pm.fill(Qt::transparent); - - QPainter painter(&pm); - renderer.render(&painter, pm.rect()); - - return pm; -} - -template -inline QIcon icon() -{ - using namespace std::literals; - - if constexpr(id >= QStyle::SP_CustomBase) { - constexpr auto data = logos::get([] { - switch(id) { - case AppIcon: - return "poi.svg"sv; - case AppWindowIcon: - return "poi_window.svg"sv; - default: - return "__unknown__"sv; - } - }); - return QIcon(render(data)); - } else { - - constexpr auto data = icons::get([] { - switch(id) { - case QStyle::SP_ArrowBack: - return "arrow-left.svg"sv; - case QStyle::SP_ArrowForward: - return "arrow-right.svg"sv; - case QStyle::SP_BrowserStop: - return "circle-x.svg"sv; - case QStyle::SP_BrowserReload: - return "refresh.svg"sv; - case QStyle::SP_DirHomeIcon: - return "home.svg"sv; - default: - return "__unknown__"sv; - } - }); - - QIcon m; - // This is a horrible hack that will one day be hopefully fixed: - // When rendering an svg, you can't actually pick a stroke color through - // QPainter::setBrush or QPainter::setPen. So instead, replace the stroke - // color in the data (stroke="currentColor") and then rerender it. - - QByteArray arr(data.data(), data.size()); - for(const auto mode : { QIcon::Normal /*, QIcon::Disabled, QIcon::Active, QIcon::Selected*/ }) { - for(const auto toggle : { QIcon::On, QIcon::Off }) { - const auto pm = render(arr.replace("currentColor", (toggle == QIcon::On ? "black" : "gray"))); - m.addPixmap(pm, mode, toggle); - } - } - return m; - } -} -} // namespace Util - -#endif // SMOLBOTE_UTIL_H diff --git a/src/webengine/CMakeLists.txt b/src/webengine/CMakeLists.txt index 5d2769d..156c64a 100644 --- a/src/webengine/CMakeLists.txt +++ b/src/webengine/CMakeLists.txt @@ -5,7 +5,7 @@ target_sources(webengine INTERFACE webview.h webview.cpp webviewcontextmenu.h webviewcontextmenu.cpp urlinterceptor.h urlinterceptor.cpp) target_include_directories(webengine INTERFACE ${CMAKE_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(webengine INTERFACE Qt5::WebEngineWidgets fmt) +target_link_libraries(webengine INTERFACE Qt5::WebEngineWidgets autogen fmt) # tests add_executable(profile_test test/profile.cpp) diff --git a/src/webengine/webviewcontextmenu.cpp b/src/webengine/webviewcontextmenu.cpp index ea5e8c6..c9d809f 100644 --- a/src/webengine/webviewcontextmenu.cpp +++ b/src/webengine/webviewcontextmenu.cpp @@ -9,11 +9,11 @@ #include "webviewcontextmenu.h" #include "webprofilemanager.h" #include "webview.h" +#include "util.h" #include #include #include #include -#include #include #include #include @@ -21,13 +21,17 @@ #include #include +constexpr int min_width = 250; +constexpr QSize button_size(32, 32); + inline QAction *historyAction(QWebEngineView *view, const QWebEngineHistoryItem &item) { - QAction *action = new QAction(view); - if(item.title().isEmpty()) + auto *action = new QAction(view); + if(item.title().isEmpty()) { action->setText(item.url().toString()); - else + } else { action->setText(QObject::tr("%1 (%2)").arg(item.title(), item.url().toString())); + } QObject::connect(action, &QAction::triggered, view, [view, item]() { view->history()->goToItem(item); @@ -38,7 +42,7 @@ inline QAction *historyAction(QWebEngineView *view, const QWebEngineHistoryItem WebViewContextMenu::WebViewContextMenu(WebView *view) : QMenu(view) { - setMinimumWidth(250); + setMinimumWidth(min_width); auto *navButtons = new QWidgetAction(this); @@ -48,8 +52,9 @@ WebViewContextMenu::WebViewContextMenu(WebView *view) buttonsLayout->setSpacing(2); auto *backButton = new QToolButton(this); + backButton->setMinimumSize(button_size); backButton->setEnabled(view->history()->canGoBack()); - backButton->setIcon(style()->standardIcon(QStyle::SP_ArrowBack)); + backButton->setIcon(Util::icon()); connect(backButton, &QToolButton::clicked, view, [this, view]() { view->back(); this->close(); @@ -57,8 +62,9 @@ WebViewContextMenu::WebViewContextMenu(WebView *view) buttonsLayout->addWidget(backButton); auto *forwardButton = new QToolButton(this); + forwardButton->setMinimumSize(button_size); forwardButton->setEnabled(view->history()->canGoForward()); - forwardButton->setIcon(style()->standardIcon(QStyle::SP_ArrowForward)); + forwardButton->setIcon(Util::icon()); connect(forwardButton, &QToolButton::clicked, view, [this, view]() { view->forward(); this->close(); @@ -66,7 +72,8 @@ WebViewContextMenu::WebViewContextMenu(WebView *view) buttonsLayout->addWidget(forwardButton); auto *refreshButton = new QToolButton(this); - refreshButton->setIcon(style()->standardIcon(QStyle::SP_BrowserReload)); + refreshButton->setMinimumSize(button_size); + refreshButton->setIcon(Util::icon()); connect(refreshButton, &QToolButton::clicked, view, [view, this]() { view->reload(); this->close(); @@ -76,12 +83,10 @@ WebViewContextMenu::WebViewContextMenu(WebView *view) buttonsLayout->addStretch(); auto *muteButton = new QToolButton(this); + muteButton->setMinimumSize(button_size); muteButton->setCheckable(true); muteButton->setChecked(view->page()->isAudioMuted()); - QIcon muteIcon; - muteIcon.addPixmap(style()->standardPixmap(QStyle::SP_MediaVolume), QIcon::Normal, QIcon::Off); - muteIcon.addPixmap(style()->standardPixmap(QStyle::SP_MediaVolumeMuted), QIcon::Normal, QIcon::On); - muteButton->setIcon(muteIcon); + muteButton->setIcon(Util::icon()); connect(muteButton, &QToolButton::clicked, view, [view, this](bool checked) { view->page()->setAudioMuted(checked); this->close(); -- cgit v1.2.1