From 582159ed03a8bec9266d120a4a9df33c352c3eed Mon Sep 17 00:00:00 2001 From: ffiirree Date: Wed, 12 Apr 2023 19:33:24 +0800 Subject: [PATCH] #25, custom ComboBox, high-dpi scaling --- languages/capturer_en_US.ts | 132 +++++++++++------------ languages/capturer_zh_CN.ts | 134 ++++++++++++------------ src/capturer.cpp | 6 +- src/capturer.h | 4 +- src/menu/editmenu.cpp | 17 --- src/menu/editmenu.h | 2 - src/menu/imageeditmenu.cpp | 2 - src/menu/menu.qss | 32 +++--- src/menu/stylemenu.cpp | 2 +- src/menu/stylemenu.h | 10 +- src/setting/settingdialog.cpp | 191 +++++++++++++++------------------- src/widgets/combobox.cpp | 1 + src/widgets/combobox.h | 62 +++++++++++ src/widgets/custombutton.cpp | 36 ------- src/widgets/custombutton.h | 40 ------- src/widgets/shortcutinput.h | 2 +- 16 files changed, 309 insertions(+), 364 deletions(-) create mode 100644 src/widgets/combobox.cpp create mode 100644 src/widgets/combobox.h delete mode 100644 src/widgets/custombutton.cpp delete mode 100644 src/widgets/custombutton.h diff --git a/languages/capturer_en_US.ts b/languages/capturer_en_US.ts index ef64e0c..1affb87 100644 --- a/languages/capturer_en_US.ts +++ b/languages/capturer_en_US.ts @@ -37,42 +37,42 @@ ImageEditMenu - + Rectangle - + Ellipse - + Arrow - + Line - + Pencil - + Text - + Mosaic - + Eraser @@ -224,205 +224,205 @@ SettingWindow - + Settings - + General - + Language - + Auto - + Dark - + Light - + Theme - - + + Screenshot - - + + - Apperance: - - - - - - Border Width - - - + + + Border Color - - - + + + Line Type - - - + + + Mask Color - + Screen Recording - - + + + + Appearance: + + + + + Show Region - - + + Params: - - + + Framerate - + Encoder - - + + High - - + + Medium - - + + Low - - + + Quality - + GIF Recording - + Devices - + Microphones - + Speakers - + Cameras - + Shortcuts - + Paste - + Hide/Show All Images - + Video Recording - + Gif Recording - + About - + Version - + Copyright © 2018 - 2023 ffiirree. All rights reserved - + Run on Startup - + Settings File diff --git a/languages/capturer_zh_CN.ts b/languages/capturer_zh_CN.ts index 2279e84..df516e4 100644 --- a/languages/capturer_zh_CN.ts +++ b/languages/capturer_zh_CN.ts @@ -37,42 +37,42 @@ ImageEditMenu - + Rectangle 矩形 - + Ellipse 椭圆 - + Arrow 箭头 - + Line 直线 - + Pencil 画笔 - + Text 文本 - + Mosaic 马赛克 - + Eraser 橡皮擦 @@ -224,205 +224,205 @@ SettingWindow - + Settings 设置 - + General 通用 - + Language 语言 - + Dark 暗色 - + Light 亮色 - + Theme 主题 - - + + Screenshot 截图 - - - - Apperance: - 界面: - - - - + + Params: 参数: - - + + Framerate 帧率 - + Microphones 麦克风 - + Speakers 扬声器 - + Cameras 摄像头 - + Encoder 编码器 - + Screen Recording 录屏 - + Run on Startup 开机启动 - + Settings File 设置文件 - + Auto 跟随系统 - - - + + + Border Width 边框宽度 - - - + + + Border Color 边框颜色 - - - + + + Line Type 线条类型 - - - + + + Mask Color 遮罩颜色 - - + + + + Appearance: + 界面: + + + + Show Region 显示区域 - - + + High - - + + Medium - - + + Low - - + + Quality 质量 - + GIF Recording 录制GIF - + Devices 设备 - + Shortcuts 快捷键 - + Paste 粘贴 - + Hide/Show All Images 隐藏/显示全部贴图 - + Video Recording 录制视频 - + Gif Recording 录制GIF - + About 关于 - + Version 版本 - + Copyright © 2018 - 2023 ffiirree. All rights reserved diff --git a/src/capturer.cpp b/src/capturer.cpp index 5ea594d..ed3b857 100644 --- a/src/capturer.cpp +++ b/src/capturer.cpp @@ -182,13 +182,13 @@ std::pair Capturer::to_pixmap(const std::pair( pixmap, QRect(platform::display::displays()[0].geometry).center() - QPoint{pixmap.width(), pixmap.height()} / 2 diff --git a/src/capturer.h b/src/capturer.h index 554ca2d..3b8f9ee 100644 --- a/src/capturer.h +++ b/src/capturer.h @@ -50,8 +50,8 @@ private slots: private: void setupSystemTrayIcon(); - std::pair clipboard_data(); - std::pair to_pixmap(const std::pair&); + static std::pair clipboard_data(); + static std::pair to_pixmap(const std::pair&); ScreenShoter * sniper_{ nullptr }; ScreenRecorder * recorder_ { nullptr }; diff --git a/src/menu/editmenu.cpp b/src/menu/editmenu.cpp index 67d7021..3dcdc5c 100644 --- a/src/menu/editmenu.cpp +++ b/src/menu/editmenu.cpp @@ -1,7 +1,6 @@ #include "editmenu.h" #include #include "separator.h" -#include "config.h" EditMenu::EditMenu(QWidget *parent) : QWidget(parent) @@ -16,22 +15,6 @@ EditMenu::EditMenu(QWidget *parent) layout()->setContentsMargins({}); } -void EditMenu::addButton(CustomButton* btn) -{ - if (Config::instance()["theme"].get() == "dark") { - btn->normal(QColor("#e6e6e6"), QColor("#232323")); - btn->hover(QColor("#e6e6e6"), QColor("#323232")); - btn->checked(Qt::white, QColor("#409eff")); - } - else { - btn->normal(QColor("#2c2c2c"), QColor("#f9f9f9")); - btn->hover(QColor("#2c2c2c"), QColor("#dfdfdf")); - btn->checked(Qt::white, QColor("#409eff")); - } - - layout()->addWidget(btn); -} - void EditMenu::addSeparator() { layout()->addWidget(new Separator()); diff --git a/src/menu/editmenu.h b/src/menu/editmenu.h index 8c837bd..3e34b99 100644 --- a/src/menu/editmenu.h +++ b/src/menu/editmenu.h @@ -3,7 +3,6 @@ #include #include -#include "custombutton.h" class EditMenu : public QWidget { @@ -31,7 +30,6 @@ class EditMenu : public QWidget protected: void moveEvent(QMoveEvent*) override; - void addButton(CustomButton*); void addSeparator(); void addWidget(QWidget *); diff --git a/src/menu/imageeditmenu.cpp b/src/menu/imageeditmenu.cpp index 9412c21..5bc9820 100644 --- a/src/menu/imageeditmenu.cpp +++ b/src/menu/imageeditmenu.cpp @@ -1,10 +1,8 @@ #include "imageeditmenu.h" #include -#include #include #include #include -#include "logging.h" ImageEditMenu::ImageEditMenu(QWidget* parent, uint32_t groups) : EditMenu(parent) diff --git a/src/menu/menu.qss b/src/menu/menu.qss index 9a36faa..3eaad8a 100644 --- a/src/menu/menu.qss +++ b/src/menu/menu.qss @@ -32,7 +32,15 @@ RecordMenu QCheckBox#stop-btn::indicator { /*///////////////////////////////// Edit Menu //////////////////////////////*/ ImageEditMenu QCheckBox { + font-size: 9pt; + max-height: 2.5em; + max-width: 2.5em; + min-height: 2.5em; + min-width: 2.5em; + spacing: 0px; + margin: 0px; + border: 0px; } ImageEditMenu QCheckBox::indicator { @@ -96,7 +104,16 @@ StyleMenu QComboBox { } StyleMenu QCheckBox { + font-size: 9pt; + + max-height: 2.5em; + max-width: 2.5em; + min-height: 2.5em; + min-width: 2.5em; + spacing: 0px; + margin: 0px; + border: 0px; } StyleMenu QCheckBox::indicator { @@ -107,27 +124,12 @@ StyleMenu QCheckBox::indicator { padding: 0.5em; } -StyleMenu QCheckBox#width-btn { - font-size: 9pt; - max-height: 2.5em; - max-width: 2.5em; - min-height: 2.5em; - min-width: 2.5em; - margin: 0px; - border: 0px; -} - StyleMenu QCheckBox#width-btn:checked { background-color: #409eff; } StyleMenu QCheckBox#width-btn::indicator { - height: 0.5em; - width: 0.5em; padding: 0px; - margin: 1em; - - border-radius: 0.245em; subcontrol-position: center center; background-color: white; diff --git a/src/menu/stylemenu.cpp b/src/menu/stylemenu.cpp index 910e3ca..50c5b54 100644 --- a/src/menu/stylemenu.cpp +++ b/src/menu/stylemenu.cpp @@ -9,7 +9,7 @@ StyleMenu::StyleMenu(int buttons, QWidget* parent) : EditMenu(parent) { - ButtonGroup* group = new ButtonGroup(this); + auto group = new ButtonGroup(this); if (buttons & WIDTH_BTN) { width_btn_ = new WidthButton(3, true, this); diff --git a/src/menu/stylemenu.h b/src/menu/stylemenu.h index b21c944..c57b0ba 100644 --- a/src/menu/stylemenu.h +++ b/src/menu/stylemenu.h @@ -16,21 +16,21 @@ class StyleMenu : public EditMenu public: explicit StyleMenu(int buttons, QWidget* parent = nullptr); - virtual void color(const QColor& c) override + void color(const QColor& c) override { color_ = c; if (color_panel_) color_panel_->setColor(color_); } - virtual void lineWidth(int w) override + void lineWidth(int w) override { width_ = std::max(1, w); if (width_btn_) width_btn_->setValue(width_); } - virtual void fill(bool fill) override + void fill(bool fill) override { fill_ = fill; @@ -42,7 +42,7 @@ class StyleMenu : public EditMenu } } - virtual QFont font() override + QFont font() override { QFont font; if (font_family_) font.setFamily(font_family_->currentText()); @@ -51,7 +51,7 @@ class StyleMenu : public EditMenu return font; } - virtual void font(const QFont& font) override + void font(const QFont& font) override { font_ = font; if (font_family_) font_family_->setCurrentText(font.family()); diff --git a/src/setting/settingdialog.cpp b/src/setting/settingdialog.cpp index 88f2a8b..46fdd35 100644 --- a/src/setting/settingdialog.cpp +++ b/src/setting/settingdialog.cpp @@ -1,14 +1,12 @@ #include "settingdialog.h" #include -#include -#include #include #include #include #include #include #include -#include +#include "combobox.h" #include "shortcutinput.h" #include "colorpanel.h" #include "titlebar.h" @@ -17,12 +15,8 @@ #include "version.h" #include "media.h" -#ifdef __linux__ -#include -#endif - SettingWindow::SettingWindow(QWidget * parent) - : QWidget(parent) + : QWidget(parent) { setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); setAttribute(Qt::WA_TranslucentBackground); @@ -89,16 +83,15 @@ void SettingWindow::setupGeneralWidget() layout->addWidget(_01, 0, 1, 1, 2); // - auto _1_2 = new QComboBox(); - _1_2->setView(new QListView()); - _1_2->view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); - _1_2->view()->window()->setAttribute(Qt::WA_TranslucentBackground); - _1_2->addItem("English", "en_US"); - _1_2->addItem("简体中文", "zh_CN"); - _1_2->setCurrentIndex(std::max(0, _1_2->findData(config["language"].get()))); - connect(_1_2, static_cast(&QComboBox::currentIndexChanged), this, [this, _1_2](int){ - config.set(config["language"], _1_2->currentData().toString()); - }); + auto _1_2 = new ComboBox(); + _1_2->add({ + {"en_US", "English"}, + {"zh_CN", "简体中文"} + }) + .select(config["language"].get()) + .onselected([this](auto value){ + config.set(config["language"], value.toString()); + }); layout->addWidget(new QLabel(tr("Language")), 1, 0, 1, 1); layout->addWidget(_1_2, 1, 1, 1, 2); @@ -110,17 +103,16 @@ void SettingWindow::setupGeneralWidget() layout->addWidget(_2_2, 2, 1, 1, 2); // - auto _3_2 = new QComboBox(); - _3_2->setView(new QListView()); - _3_2->view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); - _3_2->view()->window()->setAttribute(Qt::WA_TranslucentBackground); - _3_2->addItem(tr("Auto"), "auto"); - _3_2->addItem(tr("Dark"), "dark"); - _3_2->addItem(tr("Light"), "light"); - _3_2->setCurrentIndex(std::max(0, _3_2->findData(config["theme"].get()))); - connect(_3_2, static_cast(&QComboBox::currentIndexChanged), this, [this, _3_2](int) { - config.set_theme(_3_2->currentData().toString().toStdString()); - }); + auto _3_2 = new ComboBox(); + _3_2->add({ + {"auto", tr("Auto")}, + {"dark", tr("Dark")}, + {"light", tr("Light")} + }) + .select(config["theme"].get()) + .onselected([this](auto value) { + config.set_theme(value.toString().toStdString()); + }); layout->addWidget(new QLabel(tr("Theme")), 3, 0, 1, 1); layout->addWidget(_3_2, 3, 1, 1, 2); @@ -135,7 +127,7 @@ void SettingWindow::setupSnipWidget() auto layout = new QGridLayout(); layout->setContentsMargins(35, 10, 35, 15); - auto _0 = new QLabel(tr("Apperance:")); + auto _0 = new QLabel(tr("Appearance:")); _0->setObjectName("sub-title"); layout->addWidget(_0, 0, 1, 1, 1); @@ -155,16 +147,14 @@ void SettingWindow::setupSnipWidget() layout->addWidget(new QLabel(tr("Border Color")), 2, 1, 1, 1); layout->addWidget(_2_2, 2, 2, 1, 2); - auto _3_2 = new QComboBox(); - _3_2->setView(new QListView()); - _3_2->view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); - _3_2->view()->window()->setAttribute(Qt::WA_TranslucentBackground); - - _3_2->addItems({ "NoPen", "SolidLine", "DashLine", "DotLine", "DashDotLine", "DashDotDotLine", "CustomDashLine" }); - _3_2->setCurrentIndex(config["snip"]["selector"]["border"]["style"].get()); - connect(_3_2, static_cast(&QComboBox::currentIndexChanged), [this](int s){ - config.set(config["snip"]["selector"]["border"]["style"], s); - }); + auto _3_2 = new ComboBox(); + _3_2->add({ + "NoPen", "SolidLine", "DashLine", "DotLine", "DashDotLine", "DashDotDotLine", "CustomDashLine" + }) + .select(config["snip"]["selector"]["border"]["style"].get()) + .onselected([this](auto value){ + config.set(config["snip"]["selector"]["border"]["style"], value.toInt()); + }); layout->addWidget(new QLabel(tr("Line Type")), 3, 1, 1, 1); layout->addWidget(_3_2, 3, 2, 1, 2); @@ -184,7 +174,7 @@ void SettingWindow::setupRecordWidget() auto layout = new QGridLayout(); layout->setContentsMargins(35, 10, 35, 15); - auto _0 = new QLabel(tr("Apperance:")); + auto _0 = new QLabel(tr("Appearance:")); _0->setObjectName("sub-title"); layout->addWidget(_0, 0, 1, 1, 1); @@ -204,15 +194,14 @@ void SettingWindow::setupRecordWidget() layout->addWidget(new QLabel(tr("Border Color")), 2, 1, 1, 1); layout->addWidget(_2_2, 2, 2, 1, 2); - auto _3_2 = new QComboBox(); - _3_2->setView(new QListView()); - _3_2->view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); - _3_2->view()->window()->setAttribute(Qt::WA_TranslucentBackground); - _3_2->addItems({ "NoPen", "SolidLine", "DashLine", "DotLine", "DashDotLine", "DashDotDotLine", "CustomDashLine" }); - _3_2->setCurrentIndex(config["record"]["selector"]["border"]["style"].get()); - connect(_3_2, static_cast(&QComboBox::currentIndexChanged), [this](int s){ - config.set(config["record"]["selector"]["border"]["style"], s); - }); + auto _3_2 = new ComboBox(); + _3_2->add({ + "NoPen", "SolidLine", "DashLine", "DotLine", "DashDotLine", "DashDotDotLine", "CustomDashLine" + }) + .select(config["record"]["selector"]["border"]["style"].get()) + .onselected( [this](auto value){ + config.set(config["record"]["selector"]["border"]["style"], value.toInt()); + }); layout->addWidget(new QLabel(tr("Line Type")), 3, 1, 1, 1); layout->addWidget(_3_2, 3, 2, 1, 2); @@ -244,34 +233,34 @@ void SettingWindow::setupRecordWidget() layout->addWidget(new QLabel(tr("Framerate")), 8, 1, 1, 1); layout->addWidget(_6_2, 8, 2, 1, 2); - auto _7_2 = new QComboBox(); - _7_2->setView(new QListView()); - _7_2->view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); - _7_2->view()->window()->setAttribute(Qt::WA_TranslucentBackground); - _7_2->addItem("Software x264 [H.264 / AVC]", "libx264"); - _7_2->addItem("Software x265 [H.265 / HEVC]", "libx265"); + auto _7_2 = new ComboBox(); + _7_2->add({ + {"libx264", "Software x264 [H.264 / AVC]"}, + {"libx265", "Software x265 [H.265 / HEVC]"} + }); //if (is_support_hwdevice(AV_HWDEVICE_TYPE_CUDA)) { // _7_2->addItem("Hardware NVENC [H.264 / AVC]", "h264_nvenc"); // _7_2->addItem("Hardware NVENC [H.265 / HEVC]", "hevc_nvenc"); //} - _7_2->setCurrentIndex(std::max(0, _7_2->findData(config["record"]["encoder"].get()))); - connect(_7_2, static_cast(&QComboBox::currentIndexChanged), [this, _7_2](int) { - config.set(config["record"]["encoder"], _7_2->currentData().toString()); - }); + _7_2->select( + config["record"]["encoder"].get() + ) + .onselected([this](auto value) { + config.set(config["record"]["encoder"], value.toString()); + }); layout->addWidget(new QLabel(tr("Encoder")), 9, 1, 1, 1); layout->addWidget(_7_2, 9, 2, 1, 2); - auto _8_2 = new QComboBox(); - _8_2->setView(new QListView()); - _8_2->view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); - _8_2->view()->window()->setAttribute(Qt::WA_TranslucentBackground); - _8_2->addItem(tr("High"), "high"); - _8_2->addItem(tr("Medium"), "medium"); - _8_2->addItem(tr("Low"), "low"); - _8_2->setCurrentIndex(std::max(0, _8_2->findData(config["record"]["quality"].get()))); - connect(_8_2, static_cast(&QComboBox::currentIndexChanged), [this, _8_2](int) { - config.set(config["record"]["quality"], _8_2->currentData().toString()); - }); + auto _8_2 = new ComboBox(); + _8_2->add({ + {"high", tr("High")}, + {"medium", tr("Medium")}, + {"low", tr("Low")} + }) + .select(config["record"]["quality"].get()) + .onselected([this](auto value) { + config.set(config["record"]["quality"], value.toString()); + }); layout->addWidget(new QLabel(tr("Quality")), 10, 1, 1, 1); layout->addWidget(_8_2, 10, 2, 1, 2); @@ -287,7 +276,7 @@ void SettingWindow::setupGIFWidget() auto layout = new QGridLayout(); layout->setContentsMargins(35, 10, 35, 15); - auto _0 = new QLabel(tr("Apperance:")); + auto _0 = new QLabel(tr("Appearance:")); _0->setObjectName("sub-title"); layout->addWidget(_0, 0, 1, 1, 1); @@ -306,15 +295,14 @@ void SettingWindow::setupGIFWidget() layout->addWidget(_2_2, 2, 2, 1, 2); layout->addWidget(new QLabel(tr("Border Color")), 2, 1, 1, 1); - auto _3_2 = new QComboBox(); - _3_2->setView(new QListView()); - _3_2->view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); - _3_2->view()->window()->setAttribute(Qt::WA_TranslucentBackground); - _3_2->addItems({ "NoPen", "SolidLine", "DashLine", "DotLine", "DashDotLine", "DashDotDotLine", "CustomDashLine" }); - _3_2->setCurrentIndex(config["gif"]["selector"]["border"]["style"].get()); - connect(_3_2, static_cast(&QComboBox::currentIndexChanged), [this](int s){ - config.set(config["gif"]["selector"]["border"]["style"], s); - }); + auto _3_2 = new ComboBox(); + _3_2->add({ + "NoPen", "SolidLine", "DashLine", "DotLine", "DashDotLine", "DashDotDotLine", "CustomDashLine" + }) + .select(config["gif"]["selector"]["border"]["style"].get()) + .onselected([this](auto value){ + config.set(config["gif"]["selector"]["border"]["style"], value.toInt()); + }); layout->addWidget(_3_2, 3, 2, 1, 2); layout->addWidget(new QLabel(tr("Line Type")), 3, 1, 1, 1); @@ -346,17 +334,16 @@ void SettingWindow::setupGIFWidget() layout->addWidget(new QLabel(tr("Framerate")), 8, 1, 1, 1); layout->addWidget(_7_2, 8, 2, 1, 2); - auto _8_2 = new QComboBox(); - _8_2->setView(new QListView()); - _8_2->view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); - _8_2->view()->window()->setAttribute(Qt::WA_TranslucentBackground); - _8_2->addItem(tr("High"), "high"); - _8_2->addItem(tr("Medium"), "medium"); - _8_2->addItem(tr("Low"), "low"); - _8_2->setCurrentIndex(std::max(0, _8_2->findData(config["gif"]["quality"].get()))); - connect(_8_2, static_cast(&QComboBox::currentIndexChanged), [this, _8_2](int) { - config.set(config["gif"]["quality"], _8_2->currentData().toString()); - }); + auto _8_2 = new ComboBox(); + _8_2->add({ + {"high", tr("High")}, + {"medium", tr("Medium")}, + {"low", tr("Low")} + }) + .select(config["gif"]["quality"].get()) + .onselected([this](auto value) { + config.set(config["gif"]["quality"], value.toString()); + }); layout->addWidget(new QLabel(tr("Quality")), 9, 1, 1, 1); layout->addWidget(_8_2, 9, 2, 1, 2); @@ -371,10 +358,7 @@ void SettingWindow::setupDevicesWidget() auto layout = new QGridLayout(); layout->setContentsMargins(35, 10, 35, 15); - auto _1_2 = new QComboBox(); - _1_2->setView(new QListView()); - _1_2->view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); - _1_2->view()->window()->setAttribute(Qt::WA_TranslucentBackground); + auto _1_2 = new ComboBox(); _1_2->addItems(Devices::microphones()); layout->addWidget(new QLabel(tr("Microphones")), 1, 1, 1, 1); connect(_1_2, &QComboBox::currentTextChanged, [this](QString s) { @@ -382,10 +366,7 @@ void SettingWindow::setupDevicesWidget() }); layout->addWidget(_1_2, 1, 2, 1, 2); - auto _2_2 = new QComboBox(); - _2_2->setView(new QListView()); - _2_2->view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); - _2_2->view()->window()->setAttribute(Qt::WA_TranslucentBackground); + auto _2_2 = new ComboBox(); _2_2->addItems(Devices::speakers()); layout->addWidget(new QLabel(tr("Speakers")), 2, 1, 1, 1); connect(_2_2, &QComboBox::currentTextChanged, [this](QString s) { @@ -393,10 +374,7 @@ void SettingWindow::setupDevicesWidget() }); layout->addWidget(_2_2, 2, 2, 1, 2); - auto _3_2 = new QComboBox(); - _3_2->setView(new QListView()); - _3_2->view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); - _3_2->view()->window()->setAttribute(Qt::WA_TranslucentBackground); + auto _3_2 = new ComboBox(); _3_2->addItems(Devices::cameras()); layout->addWidget(new QLabel(tr("Cameras")), 3, 1, 1, 1); connect(_3_2, &QComboBox::currentTextChanged, [this](QString s) { @@ -484,14 +462,13 @@ void SettingWindow::setupAboutWidget() parent_layout->addWidget(copyright_); } - void SettingWindow::setAutoRun(int statue) { QString exec_path = QDir::toNativeSeparators(QCoreApplication::applicationFilePath()); #ifdef _WIN32 - QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); - settings.setValue("capturer_run", statue == Qt::Checked ? exec_path : ""); + QSettings settings(R"(HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run)", QSettings::NativeFormat); + settings.setValue("capturer_run", statue == Qt::Checked ? exec_path : ""); #elif __linux__ std::string desktop_file = "/usr/share/applications/capturer.desktop"; std::string autorun_dir = std::string{::getenv("HOME")} + "/.config/autostart"; diff --git a/src/widgets/combobox.cpp b/src/widgets/combobox.cpp new file mode 100644 index 0000000..8254016 --- /dev/null +++ b/src/widgets/combobox.cpp @@ -0,0 +1 @@ +#include "combobox.h" \ No newline at end of file diff --git a/src/widgets/combobox.h b/src/widgets/combobox.h new file mode 100644 index 0000000..16bf3f0 --- /dev/null +++ b/src/widgets/combobox.h @@ -0,0 +1,62 @@ +#ifndef COMBOBOX_H +#define COMBOBOX_H + +#include +#include + +class ComboBox : public QComboBox { + Q_OBJECT +public: + explicit ComboBox(QWidget *parent = nullptr) + : QComboBox(parent) + { + setView(new QListView()); + view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); + view()->window()->setAttribute(Qt::WA_TranslucentBackground); + + connect(this, static_cast(&QComboBox::currentIndexChanged), [this](int){ + emit selected(currentData()); + }); + } + + inline ComboBox& add(const std::map& items) + { + for (auto& [value, text] : items) { + insertItem(count(), text, value); + } + return *this; + } + + inline ComboBox& add(const QStringList &texts) + { + for (auto& text : texts) { + auto index = count(); + insertItem(index, text, index); + } + return *this; + } + + inline ComboBox& add(const QVariant& value, const QString& text) + { + insertItem(count(), text, value); + return *this; + } + + inline ComboBox& select(const QVariant& value) + { + setCurrentIndex(findData(value)); + return *this; + } + + template + inline ComboBox& onselected(Slot slot) + { + connect(this, &ComboBox::selected, std::move(slot)); + return *this; + } + +signals: + void selected(const QVariant& value); +}; + +#endif // !COMBOBOX_H \ No newline at end of file diff --git a/src/widgets/custombutton.cpp b/src/widgets/custombutton.cpp deleted file mode 100644 index 493bee7..0000000 --- a/src/widgets/custombutton.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "custombutton.h" -#include - -CustomButton::CustomButton(QWidget *parent) - : QAbstractButton (parent) -{ - connect(this, &CustomButton::toggled, [this](){ update(); }); -} - -CustomButton::CustomButton(const QSize& size, bool checkable, QWidget *parent) - : CustomButton(parent) -{ - setFixedSize(size); - setCheckable(checkable); -} - -void CustomButton::enterEvent(QEvent *) -{ - hover_ = true; -} - -void CustomButton::leaveEvent(QEvent *) -{ - hover_ = false; -} - -void CustomButton::paintEvent(QPaintEvent *) -{ - QPainter painter(this); - painter.fillRect(rect(), backgroundColor()); - - paint(&painter); - - if (!isEnabled()) - painter.fillRect(rect(), QColor{ 145, 145, 145, 95 }); -} \ No newline at end of file diff --git a/src/widgets/custombutton.h b/src/widgets/custombutton.h deleted file mode 100644 index a1f5890..0000000 --- a/src/widgets/custombutton.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef CUSTOM_BUTTON_H -#define CUSTOM_BUTTON_H - -#include - -class CustomButton : public QAbstractButton -{ -public: - explicit CustomButton(QWidget* = nullptr); - explicit CustomButton(const QSize&, bool = false, QWidget* = nullptr); - - virtual void paint(QPainter*) = 0; - - inline void normal(const QColor& icon, const QColor& bg = Qt::transparent) { icon_normal_color_ = icon, bg_normal_color_ = bg; update(); } - inline void hover(const QColor& icon, const QColor& bg = Qt::transparent) { icon_hover_color_ = icon, bg_hover_color_ = bg; update(); } - inline void checked(const QColor& icon, const QColor& bg = Qt::transparent) { icon_checked_color_ = icon, bg_checked_color_ = bg; update(); } - - inline QColor backgroundColor() const { return isChecked() ? bg_checked_color_ : ((hover_ && isEnabled()) ? bg_hover_color_ : bg_normal_color_); } - inline QColor iconColor() const { return isChecked() ? icon_checked_color_ : ((hover_ && isEnabled()) ? icon_hover_color_ : icon_normal_color_); } - -protected: - void paintEvent(QPaintEvent*) override; - void enterEvent(QEvent*) override; - void leaveEvent(QEvent*) override; - -protected: - bool hover_{ false }; - - // icon - QColor icon_normal_color_{ 0x38, 0x38, 0x38 }; - QColor icon_hover_color_{ 0x38, 0x38, 0x38 }; - QColor icon_checked_color_{ 0x38, 0x38, 0x38 }; - - // background - QColor bg_normal_color_{ Qt::transparent }; - QColor bg_hover_color_{ Qt::transparent }; - QColor bg_checked_color_{ Qt::transparent }; -}; - -#endif // CUSTOM_BUTTON_H diff --git a/src/widgets/shortcutinput.h b/src/widgets/shortcutinput.h index 02315e9..312f98f 100644 --- a/src/widgets/shortcutinput.h +++ b/src/widgets/shortcutinput.h @@ -14,7 +14,7 @@ class ShortcutInput : public QLineEdit inline void set(const QString& str) { setText(str); emit changed(QKeySequence(str)); } inline void set(const QKeySequence& ks) { setText(ks.toString()); emit changed(ks); } - inline QKeySequence get() const { return QKeySequence(this->text()); } + [[nodiscard]] inline QKeySequence get() const { return text(); } signals: void changed(const QKeySequence&);