Commit 194f317c authored by captainwong's avatar captainwong

jlibqt IconBtn, BgColorBtn ok

parent d978d650
#include "ThreadCtrl.h"
namespace jlib
{
namespace qt
{
//JLIBQT_NAMESPACE_BEGIN
ThreadCtrl::ThreadCtrl(QObject* parent, int proto_type)
: QThread(parent)
......@@ -25,5 +22,4 @@ void ThreadCtrl::run()
emit sig_done(tag_, result_code_);
}
}
}
//JLIBQT_NAMESPACE_END
#pragma once
#include "../qt_global.h"
#include <QThread>
#include "../Model/ThreadModel.h"
#include <QThread>
namespace jlib
{
namespace qt
{
//JLIBQT_NAMESPACE_BEGIN
class ThreadCtrl : public QThread
{
......@@ -44,5 +41,4 @@ private:
int tag_ = -1;
};
}
}
//JLIBQT_NAMESPACE_END
#pragma once
#include "qt_global.h"
#include "../qt_global.h"
//#include <system_error>
#include <QString>
#include <functional>
namespace jlib
{
namespace qt
{
typedef std::function<int(void)> ThreadWorker;
......@@ -22,8 +18,6 @@ struct ThreadProgress {
typedef std::function<void(ThreadProgress)> ThreadProgressCB;
}
}
//Q_DECLARE_METATYPE(std::error_code)
Q_DECLARE_METATYPE(jlib::qt::ThreadProgress)
Q_DECLARE_METATYPE(ThreadProgress)
......@@ -21,6 +21,11 @@ struct PathHelper
return QDateTime::currentDateTime().toString(JLIBQT_FILE_TIME_FORMAT);
}
static inline QString getFolder(const QString& path) {
QDir dir(path); dir.cdUp();
return dir.absolutePath();
}
virtual ~PathHelper() {}
virtual QString program() const { return programPath_; }
......
#include "BgColorBtn.h"
#include <jlib/qt/QtStylesheet.h>
namespace HBVideoPlatform {
namespace common {
//namespace HBVideoPlatform {
//namespace common {
BgColorBtn::BgColorBtn(QWidget *parent)
: QPushButton(parent)
......@@ -60,5 +60,5 @@ void BgColorBtn::slot_clicked()
}
}
}
}
//}
//}
#pragma once
#include <QPushButton>
#include "../QtStylesheet.h"
namespace HBVideoPlatform {
namespace common {
//namespace HBVideoPlatform {
//namespace common {
class BgColorBtn : public QPushButton
{
......@@ -13,7 +14,10 @@ public:
BgColorBtn(QWidget *parent = nullptr);
~BgColorBtn();
void set_btn_attr(QColor bg_normal, QColor bg_suspend, QColor font_color, unsigned int font_sz);
void set_btn_attr(QColor bg_normal = jlib::qt::def_colors::bg_normal,
QColor bg_suspend = jlib::qt::def_colors::bg_suspend,
QColor font_color = jlib::qt::def_colors::control_text_font,
unsigned int font_sz = 12);
void set_tag(int tag) { tag_ = tag; }
int tag() const { return tag_; }
......@@ -42,5 +46,5 @@ private:
int tag_ = -1;
};
}
}
//}
//}
......@@ -11,8 +11,8 @@
class QNetworkAccessManager;
class QNetworkReply;
namespace HBVideoPlatform {
namespace common {
//namespace HBVideoPlatform {
//namespace common {
class HttpDlg : public QDialog
{
......@@ -101,5 +101,5 @@ private:
};
}
}
//}
//}
#pragma once
#include <QString>
#include <QObject>
#include "../ErrorCode.h"
namespace jlib
{
namespace qt
{
enum class HttpDlgErrorCode {
NetworkError = 1,
ParseJsonError,
};
}
}
namespace std {
ENABLE_ENUM_AS_ERROR_CODE(jlib::qt::HttpDlgErrorCode);
}
namespace
{
class HttpDlgErrorCategory : public std::error_category
{
public:
const char* name() const noexcept override {
return "HttpClient";
}
std::string message(int ev) const override {
switch (static_cast<jlib::qt::HttpDlgErrorCode>(ev)) {
case jlib::qt::HttpDlgErrorCode::NetworkError:
return QObject::tr("Network Error").toLocal8Bit().toStdString();
break;
case jlib::qt::HttpDlgErrorCode::ParseJsonError:
return QObject::tr("Cannot Parse Json").toLocal8Bit().toStdString();
break;
default:
return QObject::tr("Unkown Error").toLocal8Bit().toStdString();
break;
}
}
};
static const HttpDlgErrorCategory theHttpDlgErrorCategory = {};
}
inline std::error_code std::make_error_code(jlib::qt::HttpDlgErrorCode ec)
{
return { static_cast<int>(ec), theHttpDlgErrorCategory };
}
\ No newline at end of file
#include "IconBtn.h"
#include <QtGui/QMouseEvent>
#include <QtGui/QBitMap>
#include <QtGui/QPainter>
#include <QDebug>
#include <QTimer>
#include <jlib/qt/QtUtils.h>
#include <QPainter>
#include <QStylePainter>
#include <QStyleOption>
#include <QMouseEvent>
#include "../QtUtils.h"
#include "../QtStylesheet.h"
namespace HBVideoPlatform {
namespace common {
using namespace jlib::qt;
IconBtn::IconBtn(QWidget *parent, QString icon_path, uint32_t state_set)
IconBtn::IconBtn(QWidget* parent, QString icon_path, int tag)
: QLabel(parent)
, icon_path_(icon_path)
, state_set_(state_set)
, tag_(tag)
{
setAttribute(Qt::WA_TranslucentBackground, true);
long_press_timer_ = new QTimer(this);
connect(long_press_timer_, SIGNAL(timeout()), this, SLOT(slot_long_press_timeout()));
state_set_ |= IconStatus::status_normal;
set_icon_path(icon_path_);
setStyleSheet(build_style(Qt::white, 18));
if (icon_path.isEmpty()) {
icon_path_.clear();
} else {
icon_path_[IconStatus::status_normal] = icon_path + "_n.png";
icon_path_[IconStatus::status_hover] = icon_path + "_h.png";
icon_path_[IconStatus::status_press] = icon_path + "_p.png";
icon_path_[IconStatus::status_disable] = icon_path + "_d.png";
icon_path_[IconStatus::status_ing] = icon_path + "_ing.png";
}
refresh();
}
IconBtn::~IconBtn()
{
}
void IconBtn::set_pos(const QPoint & pos)
{
move(pos);
}
void IconBtn::set_pos(int x, int y)
{
set_pos(QPoint(x, y));
}
void IconBtn::set_icon_path(const QString & icon_path)
void IconBtn::set_icon(IconStatus status, QString icon_path)
{
icon_path_ = icon_path;
refresh_icon_status();
icon_path_[status] = icon_path;
refresh();
}
void IconBtn::set_enabled(bool enable)
void IconBtn::set_icon(QString icon_path)
{
is_enable_ = enable;
refresh_icon_status();
icon_path_[IconStatus::status_normal] = icon_path + "_n.png";
icon_path_[IconStatus::status_hover] = icon_path + "_h.png";
icon_path_[IconStatus::status_press] = icon_path + "_p.png";
icon_path_[IconStatus::status_disable] = icon_path + "_d.png";
icon_path_[IconStatus::status_ing] = icon_path + "_ing.png";
refresh();
}
void IconBtn::set_ing_status(bool is_ing)
void IconBtn::set_highlight(bool on)
{
is_ing_status_ = is_ing;
refresh_icon_status();
is_highlight_ = on;
refresh();
}
void IconBtn::enterEvent(QEvent *)
void IconBtn::enterEvent(QEvent*)
{
if (!is_enable_) { return; }
if (!is_enabled_ || !isEnabled() || (parent() && !parentWidget()->isEnabled()))return;
setCursor(QCursor(Qt::PointingHandCursor));
is_mouse_hover_ = true;
refresh_icon_status();
is_hover_ = true;
setFocus();
refresh();
emit sig_focus_on(tag_, true);
emit sig_mouse_enter();
}
void IconBtn::leaveEvent(QEvent *)
void IconBtn::leaveEvent(QEvent*)
{
if (!is_enabled_ || !isEnabled() || (parent() && !parentWidget()->isEnabled()))return;
setCursor(QCursor(Qt::ArrowCursor));
is_mouse_hover_ = false;
is_mouse_press_ = false;
refresh_icon_status();
is_hover_ = false;
is_press_ = false;
refresh();
emit sig_mouse_leave();
}
void IconBtn::mousePressEvent(QMouseEvent *)
void IconBtn::mousePressEvent(QMouseEvent*)
{
if (!is_enable_) { return; }
is_mouse_press_ = true;
refresh_icon_status();
if (state_set_ & type_long_press) {
if (long_press_timer_->isActive()) {
long_press_timer_->stop();
}
long_press_timer_->setSingleShot(true);
long_press_timer_->start(450);
}
if (!is_enabled_ || !isEnabled() || (parent() && !parentWidget()->isEnabled()))return;
is_press_ = true;
setFocus();
refresh();
}
void IconBtn::mouseReleaseEvent(QMouseEvent * e)
void IconBtn::mouseReleaseEvent(QMouseEvent* e)
{
if (!is_enable_) { return; }
if (long_press_timer_->isActive()) {
long_press_timer_->stop();
}
bool is_long_press = is_long_press_;
is_long_press_ = is_mouse_press_ = false;
refresh_icon_status();
if (!is_enabled_ || !isEnabled() || (parent() && !parentWidget()->isEnabled()))return;
is_press_ = false;
refresh();
if (Qt::LeftButton == e->button()) {
if (rect().contains(e->pos())) {
is_long_press ? emit long_press_trigger(false) : emit clicked();
MYQDEBUG << "IconBtn emit clicked";
emit clicked();
emit sig_clicked(tag_);
emit sig_clicked_with_data(tag_, data_);
}
}
}
void IconBtn::refresh_icon_status()
void IconBtn::refresh()
{
QString icon_path;
if (!is_enable_) {
if (!(state_set_ & status_disable)) { return; }
icon_path = icon_path_ + "_d.png";
} else if (is_mouse_press_) {
if (!(state_set_ & status_press)) { return; }
icon_path = icon_path_ + "_h.png";
} else if (is_ing_status_) {
if (!(state_set_ & status_ing)) { return; }
icon_path = icon_path_ + "_ing.png";
} else if (is_mouse_hover_) {
if (!(state_set_ & status_hover)) { return; }
icon_path = icon_path_ + "_p.png";
} else {
if (!(state_set_&status_normal)) { return; }
icon_path = icon_path_ + "_n.png";
if (!is_enabled_ && icon_path_.find(IconStatus::status_disable) != icon_path_.end()) {
icon_path = icon_path_[IconStatus::status_disable];
} else if (is_press_ && icon_path_.find(IconStatus::status_press) != icon_path_.end()) {
icon_path = icon_path_[IconStatus::status_press];
} else if ((is_hover_ || is_highlight_) && icon_path_.find(IconStatus::status_hover) != icon_path_.end()) {
icon_path = icon_path_[IconStatus::status_hover];
} else if (icon_path_.find(IconStatus::status_normal) != icon_path_.end()) {
icon_path = icon_path_[IconStatus::status_normal];
}
if (!icon_path.isEmpty()) {
QPixmap pixmap;
if (!LOAD_PIXMAP_EX(icon_path)) {
return;
}
setFixedSize(pixmap.size());
LOAD_PIXMAP_EX(icon_path);
if (sz_.isValid()) {
resize(sz_);
QSize pixSize = pixmap.size();
pixSize.scale(sz_, Qt::IgnoreAspectRatio);
auto spixmap = pixmap.scaled(pixSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
setPixmap(spixmap);
} else {
resize(pixmap.size());
setPixmap(pixmap);
if (state_set_ & type_mask) {
setMask(pixmap.mask());
}
if (state_set_ & type_opaque_paint) {
setAttribute(Qt::WA_OpaquePaintEvent, true);
}
}
void IconBtn::slot_long_press_timeout()
{
is_long_press_ = true;
emit long_press_trigger(true);
}
}
update();
}
#pragma once
#include <QLabel>
namespace HBVideoPlatform {
namespace common {
#include <unordered_map>
class IconBtn : public QLabel
{
Q_OBJECT
public:
enum IconStatus : uint32_t {
enum class IconStatus : uint32_t {
status_normal = 0x00000001,
status_hover = 0x00000002,
status_press = 0x00000004,
......@@ -27,15 +25,28 @@ public:
status_default = status_normal | status_hover | status_press,
};
IconBtn(QWidget *parent, QString icon_path, uint32_t state_set = IconStatus::status_default);
public:
IconBtn(QWidget* parent = nullptr, QString iconPath = "", int tag = -1);
~IconBtn();
void set_pos(const QPoint& pos);
void set_pos(int x, int y);
void set_icon_path(const QString& icon_path);
void set_enabled(bool enable);
void set_ing_status(bool is_ing);
bool is_ing_status() const { return is_ing_status_; }
void set_icon(IconStatus status, QString icon_path);
void set_icon(QString icon_path);
void set_tag(int tag) { tag_ = tag; }
int tag() const { return tag_; }
void set_data(int data) { data_ = data; }
int data() const { return data_; }
void set_highlight(bool on = true);
void set_size(QSize sz) { sz_ = sz; setFixedSize(sz); refresh(); }
void set_pos(const QPoint& pos){ move(pos); }
void set_pos(int x, int y){ move(x, y); }
void set_enabled(bool enabled) { is_enabled_ = enabled; refresh(); }
void set_ing(bool is_ing) { is_ing_ = is_ing; refresh(); }
bool is_ing() const { return is_ing_; }
protected:
virtual void enterEvent(QEvent*) override;
......@@ -43,28 +54,24 @@ protected:
virtual void mousePressEvent(QMouseEvent*) override;
virtual void mouseReleaseEvent(QMouseEvent*) override;
void refresh();
signals:
void clicked();
void long_press_trigger(bool is_press);
void sig_focus_on(int tag, bool on);
void sig_clicked(int tag);
void sig_clicked_with_data(int tag, int data);
void sig_mouse_enter();
void sig_mouse_leave();
private slots:
void slot_long_press_timeout();
private:
unsigned long state_set_ = 0;
QString icon_path_ = {};
bool is_enable_ = true;
bool is_ing_status_ = false;
bool is_mouse_hover_ = false;
bool is_mouse_press_ = false;
QTimer* long_press_timer_ = {};
bool is_long_press_ = false;
void refresh_icon_status();
protected:
std::unordered_map<IconStatus, QString> icon_path_ = {};
bool is_enabled_ = true;
bool is_hover_ = false;
bool is_press_ = false;
bool is_ing_ = false;
bool is_highlight_ = false;
int tag_ = -1;
int data_ = 0;
QSize sz_ = {};
};
}
}
......@@ -7,8 +7,7 @@
#include <vector>
#include <qelapsedtimer.h>
namespace HBVideoPlatform {
namespace common {
//JLIBQT_NAMESPACE_BEGIN
class LoadingView : public QDialog
{
......@@ -28,7 +27,7 @@ public:
private slots:
void slot_ready(int tag, std::error_code ec);
void slot_progress(int tag, HBVideoPlatform::common::ThreadProgress progress);
void slot_progress(int tag, ThreadProgress progress);
protected:
virtual void timerEvent(QTimerEvent* e) override;
......@@ -41,12 +40,11 @@ private:
QLabel* progress1_ = {};
QLabel* progress2_ = {};
QLabel* elapse_ = {};
std::vector<HBVideoPlatform::common::ThreadProgress> tp_ = {};
std::vector<ThreadProgress> tp_ = {};
bool show_progress_ = false;
LoadingViewSize sz_ = sz_big;
QElapsedTimer timer_ = {};
};
}
}
//JLIBQT_NAMESPACE_END
#include "PageTitle.h"
#include <QLayout>
#include <QPixmap>
#include <QApplication>
#include "../QtStylesheet.h"
using namespace jlib::qt;
PageTitle::PageTitle(QWidget* parent,
QString minIcon,
QString maxIcon,
QString restoreIcon,
QString closeIcon,
QString logoIcon)
: QWidget(parent)
, minIcon_(minIcon)
, maxIcon_(maxIcon)
, restoreIcon_(restoreIcon)
, closeIcon_(closeIcon)
, logoIcon_(logoIcon)
{
setAutoFillBackground(true);
QPalette pal = palette();
pal.setColor(QPalette::Window, Qt::black);
setPalette(pal);
minimize_ = new IconBtn(this, minIcon);
maximize_ = new IconBtn(this, maxIcon);
close_ = new IconBtn(this, closeIcon);
connect(minimize_, SIGNAL(clicked()), this, SLOT(slot_minimize()));
connect(maximize_, SIGNAL(clicked()), this, SLOT(slot_maximize_or_restore()));
connect(close_, SIGNAL(clicked()), this, SLOT(slot_close()));
logo_ = new QLabel(this);
QPixmap pixmap;
pixmap.load(logoIcon);
logo_->setFixedSize(32, 32);
logo_->setPixmap(pixmap.scaled(32, 32, Qt::KeepAspectRatio));
title_ = new QLabel(this);
//title_->setStyleSheet(def_style_sheets::title);
title_->setStyleSheet(build_style(def_colors::font_normal3, 18));
layout_ = new QHBoxLayout(this);
layout_->addWidget(logo_);
layout_->addSpacing(5);
layout_->addWidget(title_);
layout_->addStretch();
layout_->addWidget(minimize_);
layout_->addWidget(maximize_);
layout_->addWidget(close_);
layout_->setSpacing(0);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setMinimumHeight(7);
lastTimeClick.start();
}
PageTitle::~PageTitle()
{
}
void PageTitle::set_title(QString title)
{
title_->setText(title);
}
void PageTitle::set_maximize_btn_visible(bool visible)
{
visible ? maximize_->show() : maximize_->hide();
}
void PageTitle::set_maximized(bool isMax)
{
is_maximized_ = isMax;
if (isMax) {
maximize_->set_icon(restoreIcon_);
parentWidget()->showMaximized();
} else {
maximize_->set_icon(maxIcon_);
parentWidget()->showNormal();
}
}
void PageTitle::mousePressEvent(QMouseEvent* e)
{
if (!rect().contains(e->pos()))return;
is_mouse_pressed_ = true;
mouse_pressed_pos_ = mapToParent(e->pos());
}
void PageTitle::mouseMoveEvent(QMouseEvent* e)
{
if (!is_mouse_pressed_)return;
if (is_maximized_) {
maximize_->set_icon(maxIcon_);
parentWidget()->showNormal();
is_maximized_ = false;
}
parentWidget()->move(e->globalPos() - mouse_pressed_pos_);
}
void PageTitle::mouseReleaseEvent(QMouseEvent* e)
{
is_mouse_pressed_ = false;
if (lastTimeClick.elapsed() < QApplication::doubleClickInterval()) {
slot_maximize_or_restore();
}
lastTimeClick.start();
}
void PageTitle::slot_minimize()
{
parentWidget()->showMinimized();
}
void PageTitle::slot_maximize_or_restore()
{
if (is_maximized_) {
maximize_->set_icon(maxIcon_);
parentWidget()->showNormal();
is_maximized_ = false;
} else {
maximize_->set_icon(restoreIcon_);
parentWidget()->showMaximized();
is_maximized_ = true;
}
emit sig_maximized(is_maximized_);
}
void PageTitle::slot_close()
{
emit sig_close();
}
......@@ -14,103 +14,36 @@
#include "IconBtn.h"
#include "../QtStylesheet.h"
namespace jlib
{
namespace qt
{
//namespace jlib
//{
//namespace qt
//{
class PageTitle : public QWidget
{
Q_OBJECT
public:
PageTitle(QWidget *parent, QString logo_path)
: QWidget(parent)
{
setAutoFillBackground(true);
QPalette pal = palette();
pal.setColor(QPalette::Window, Qt::black);
setPalette(pal);
minimize_ = new IconBtn(this, "Skin/system/min");
maximize_ = new IconBtn(this, "Skin/system/max");
close_ = new IconBtn(this, "Skin/system/close");
connect(minimize_, SIGNAL(clicked()), this, SLOT(slot_minimize()));
connect(maximize_, SIGNAL(clicked()), this, SLOT(slot_maximize_or_restore()));
connect(close_, SIGNAL(clicked()), this, SLOT(slot_close()));
PageTitle(QWidget* parent = NULL,
QString minIcon = ":/Skin/system/min",
QString maxIcon = ":/Skin/system/max",
QString restoreIcon = ":/Skin/system/restore",
QString closeIcon = ":/Skin/system/close",
QString logoIcon = ":/Skin/favicon.png");
logo_ = new QLabel(this);
QPixmap pixmap;
pixmap.load(logo_path);
logo_->setFixedSize(32, 32);
logo_->setPixmap(pixmap.scaled(32, 32, Qt::KeepAspectRatio));
title_ = new QLabel(this);
//title_->setStyleSheet(def_style_sheets::title);
title_->setStyleSheet(build_style(def_colors::font_normal3, 18));
layout_ = new QHBoxLayout(this);
layout_->addWidget(logo_);
layout_->addSpacing(5);
layout_->addWidget(title_);
layout_->addStretch();
layout_->addWidget(minimize_);
layout_->addWidget(maximize_);
layout_->addWidget(close_);
layout_->setSpacing(0);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setMinimumHeight(7);
lastTimeClick.start();
}
~PageTitle();
public:
void set_title(QString title) {
title_->setText(title);
}
void set_maximize_btn_visible(bool visible) {
visible ? maximize_->show() : maximize_->hide();
}
void set_maximized(bool isMax = true) {
is_maximized_ = isMax;
if (isMax) {
maximize_->set_icon_path("Skin/system/restore");
parentWidget()->showMaximized();
} else {
maximize_->set_icon_path("Skin/system/max");
parentWidget()->showNormal();
}
}
protected:
virtual void mousePressEvent(QMouseEvent* e) override {
if (!rect().contains(e->pos()))return;
is_mouse_pressed_ = true;
mouse_pressed_pos_ = mapToParent(e->pos());
}
void set_title(QString title);
void set_maximize_btn_visible(bool visible);
void set_maximized(bool isMax = true);
virtual void mouseMoveEvent(QMouseEvent* e) override {
if (!is_mouse_pressed_)return;
if (is_maximized_) {
maximize_->set_icon_path("Skin/system/max");
parentWidget()->showNormal();
is_maximized_ = false;
}
parentWidget()->move(e->globalPos() - mouse_pressed_pos_);
}
virtual void mouseReleaseEvent(QMouseEvent* e) override {
is_mouse_pressed_ = false;
if (lastTimeClick.elapsed() < QApplication::doubleClickInterval()) {
slot_maximize_or_restore();
}
lastTimeClick.start();
}
protected:
virtual void mousePressEvent(QMouseEvent* e) override;
virtual void mouseMoveEvent(QMouseEvent* e) override;
virtual void mouseReleaseEvent(QMouseEvent* e) override;
signals:
void sig_close();
......@@ -132,32 +65,13 @@ private:
bool is_mouse_pressed_ = false;
QPoint mouse_pressed_pos_ = {};
QElapsedTimer lastTimeClick = {};
};
inline void PageTitle::slot_minimize()
{
parentWidget()->showMinimized();
}
inline void PageTitle::slot_maximize_or_restore()
{
if (is_maximized_) {
maximize_->set_icon_path("Skin/system/max");
parentWidget()->showNormal();
is_maximized_ = false;
} else {
maximize_->set_icon_path("Skin/system/restore");
parentWidget()->showMaximized();
is_maximized_ = true;
}
emit sig_maximized(is_maximized_);
}
inline void PageTitle::slot_close()
{
emit sig_close();
}
QString minIcon_ = {};
QString maxIcon_ = {};
QString restoreIcon_ = {};
QString closeIcon_ = {};
QString logoIcon_ = {};
};
}
}
//}
//}
#include "RndButton.h"
#include <jlib/qt/QtStylesheet.h>
#include "../QtStylesheet.h"
using namespace jlib::qt;
namespace HBVideoPlatform {
namespace common {
RndButton::RndButton(QWidget *parent)
RndButton::RndButton(QWidget* parent)
: QWidget(parent)
{
txt_ = new QLabel(this);
......@@ -26,7 +25,7 @@ void RndButton::set_attr(QString txt, QSize sz, int font_size)
setFixedSize(sz);
/*QPixmap pixmap;
LOAD_PIXMAP_EX(QString::fromLocal8Bit("Skin/Ӧÿ1.png"));
LOAD_PIXMAP_EX(QString::fromLocal8Bit(":/Skin/Ӧÿ1.png"));
QSize pixSize = pixmap.size();
pixSize.scale(sz, Qt::KeepAspectRatio);
pixmap_ = pixmap.scaled(pixSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);*/
......@@ -47,7 +46,7 @@ void RndButton::set_highlight(bool on)
update();
}
void RndButton::paintEvent(QPaintEvent * e)
void RndButton::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
......@@ -60,15 +59,19 @@ void RndButton::paintEvent(QPaintEvent * e)
painter.drawPath(path);
}
void RndButton::enterEvent(QEvent * e)
void RndButton::enterEvent(QEvent* e)
{
if (!isEnabled()) { return; }
setCursor(QCursor(Qt::PointingHandCursor));
bk_color_ = Qt::darkGray;
update();
emit sig_focus_on();
}
void RndButton::leaveEvent(QEvent * e)
void RndButton::leaveEvent(QEvent* e)
{
if (!isEnabled()) { return; }
setCursor(QCursor(Qt::ArrowCursor));
bk_color_ = is_highlighted_ ? Qt::lightGray : def_colors::control_bk;
......@@ -77,16 +80,18 @@ void RndButton::leaveEvent(QEvent * e)
is_pressed_ = false;
}
void RndButton::mousePressEvent(QMouseEvent * e)
void RndButton::mousePressEvent(QMouseEvent* e)
{
if (e->button() != Qt::LeftButton)return;
bk_color_ = def_colors::control_bk;
update();
is_pressed_ = true;
}
void RndButton::mouseReleaseEvent(QMouseEvent * e)
void RndButton::mouseReleaseEvent(QMouseEvent* e)
{
if (e->button() != Qt::LeftButton)return;
bk_color_ = Qt::darkGray;
update();
......@@ -96,5 +101,3 @@ void RndButton::mouseReleaseEvent(QMouseEvent * e)
}
}
}
}
......@@ -2,8 +2,8 @@
#include <QtWidgets>
namespace HBVideoPlatform {
namespace common {
//namespace HBVideoPlatform {
//namespace common {
class RndButton : public QWidget
{
......@@ -14,7 +14,6 @@ public:
~RndButton();
void set_attr(QString txt, QSize btn_sz = { 60, 30 }, int font_size = 14);
void set_highlight(bool on = true);
protected:
......@@ -26,6 +25,7 @@ protected:
signals:
void clicked();
void sig_focus_on();
private:
QLabel* txt_ = {};
......@@ -37,5 +37,5 @@ private:
bool is_highlighted_ = false;
};
}
}
//}
//}
......@@ -30,9 +30,11 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<TargetName>jlibqt</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<TargetName>jlibqt</TargetName>
</PropertyGroup>
<Target Name="QtMsBuildNotFound" BeforeTargets="CustomBuild;ClCompile" Condition="!Exists('$(QtMsBuild)\qt.targets') or !Exists('$(QtMsBuild)\qt.props')">
<Message Importance="High" Text="QtMsBuild: could not locate qt.targets, qt.props; project may not build correctly." />
......@@ -51,7 +53,7 @@
</ImportGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<QtInstall>5.9.8</QtInstall>
<QtModules>core;gui;widgets</QtModules>
<QtModules>core;gui;network;widgets</QtModules>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<QtInstall>5.9.8</QtInstall>
......@@ -90,21 +92,19 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Ctrl\ThreadCtrl.cpp" />
<ClCompile Include="qt.cpp" />
<ClCompile Include="View\BgColorBtn.cpp" />
<ClCompile Include="View\IconBtn.cpp" />
<ClCompile Include="View\PageTitle.cpp" />
<ClCompile Include="View\RndButton.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h" />
<ClInclude Include="View\HttpDlgErrorCode.h" />
<QtMoc Include="View\RndButton.h" />
<QtMoc Include="View\PageTitle.h" />
<QtMoc Include="View\LoadingView.h" />
<QtMoc Include="View\IconBtn.h" />
<QtMoc Include="View\HttpDlg.h" />
<QtMoc Include="View\BgColorBtn.h" />
<QtMoc Include="Ctrl\ThreadCtrl.h" />
<ClInclude Include="ErrorCode.h" />
<ClInclude Include="Model\ThreadModel.h" />
<ClInclude Include="monitor.h" />
<ClInclude Include="qt.h" />
<ClInclude Include="QtDebug.h" />
......
......@@ -33,8 +33,17 @@
<ClCompile Include="qt.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Ctrl\ThreadCtrl.cpp">
<Filter>Ctrl</Filter>
<ClCompile Include="View\RndButton.cpp">
<Filter>View</Filter>
</ClCompile>
<ClCompile Include="View\PageTitle.cpp">
<Filter>View</Filter>
</ClCompile>
<ClCompile Include="View\IconBtn.cpp">
<Filter>View</Filter>
</ClCompile>
<ClCompile Include="View\BgColorBtn.cpp">
<Filter>View</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
......@@ -65,12 +74,6 @@
<ClInclude Include="signal_slot.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Model\ThreadModel.h">
<Filter>Model</Filter>
</ClInclude>
<ClInclude Include="View\HttpDlgErrorCode.h">
<Filter>View</Filter>
</ClInclude>
<ClInclude Include="resource.h">
<Filter>Header Files</Filter>
</ClInclude>
......@@ -81,21 +84,12 @@
</ClInclude>
</ItemGroup>
<ItemGroup>
<QtMoc Include="Ctrl\ThreadCtrl.h">
<Filter>Ctrl</Filter>
</QtMoc>
<QtMoc Include="View\BgColorBtn.h">
<Filter>View</Filter>
</QtMoc>
<QtMoc Include="View\HttpDlg.h">
<Filter>View</Filter>
</QtMoc>
<QtMoc Include="View\IconBtn.h">
<Filter>View</Filter>
</QtMoc>
<QtMoc Include="View\LoadingView.h">
<Filter>View</Filter>
</QtMoc>
<QtMoc Include="View\PageTitle.h">
<Filter>View</Filter>
</QtMoc>
......
#pragma once
#include <QThread>
#include "../Model/ThreadModel.h"
#include <thread>
namespace jlib
{
namespace qt
{
class ThreadCtrl : public QThread
{
Q_OBJECT
public:
ThreadCtrl(QObject *parent, ThreadWorker worker)
: QThread(parent)
, worker_(worker)
{
}
~ThreadCtrl() {}
void set_worker(ThreadWorker worker) { worker_ = worker; }
void set_tag(int tag) { tag_ = tag; }
int get_tag() const { return tag_; }
protected:
virtual void run() override {
std::error_code result;
if (worker_) {
result = worker_();
}
emit sig_ready(tag_, result);
}
signals:
void sig_ready(int tag, std::error_code result);
void sig_progress(int tag, jlib::qt::ThreadProgress progress);
private:
ThreadWorker worker_ = {};
void* input_ = nullptr;
void* output_ = nullptr;
int tag_ = 0;
};
}
}
#pragma once
#include <system_error>
#include <QString>
namespace jlib
{
namespace qt
{
//! 获取错误码文字描述
inline QString ecString(const std::error_code& ec) {
return QString::fromLocal8Bit(ec.message().data());
}
//! 获取错误码文字描述(带有错误目录)
inline QString ecStringWithCategory(const std::error_code& ec) {
return QString::fromLocal8Bit(ec.category().name()) + ": " + QString::fromLocal8Bit(ec.message().data());
}
#define BREAK_IF_QUERY_FAILED(ec_type) if (!ok) { \
ec = ec_type; \
MYQCRITICAL << ecStringWithCategory(ec) << "\n" << query.lastError(); \
break; \
}
enum class CommonErrorCode {
OpenDbFailed = 1,
CreateDbTableFailed,
QueryDbFailed,
};
}
}
namespace std
{
#define ENABLE_ENUM_AS_ERROR_CODE(type) \
template <> struct is_error_code_enum<type> : true_type {}; \
std::error_code make_error_code(type);
ENABLE_ENUM_AS_ERROR_CODE(jlib::qt::CommonErrorCode)
}
namespace
{
struct CommonErrorCatagory : public std::error_category
{
const char* name() const noexcept override {
return "Common Error";
}
std::string message(int ev) const override {
switch (static_cast<jlib::qt::CommonErrorCode>(ev)) {
case jlib::qt::CommonErrorCode::OpenDbFailed:
return QObject::tr("Failed to open database").toLocal8Bit().toStdString();
case jlib::qt::CommonErrorCode::CreateDbTableFailed:
return QObject::tr("Failed to create table").toLocal8Bit().toStdString();
case jlib::qt::CommonErrorCode::QueryDbFailed:
return QObject::tr("Failed to query database").toLocal8Bit().toStdString();
default:
return QObject::tr("Unkown Error").toLocal8Bit().toStdString();
}
}
};
static const CommonErrorCatagory theCommonErrorCatagory = {};
}
inline std::error_code std::make_error_code(jlib::qt::CommonErrorCode ec)
{
return { static_cast<int>(ec), theCommonErrorCatagory };
}
#pragma once
#include <QString>
#include <functional>
namespace jlib
{
namespace qt
{
typedef std::function<std::error_code(void)> ThreadWorker;
struct ThreadProgress {
int pos = 0;
int total = 0;
ThreadProgress() {}
ThreadProgress(int p, int t) :pos(p), total(t) {}
};
typedef std::function<void(ThreadProgress)> ThreadProgressCB;
}
}
Q_DECLARE_METATYPE(std::error_code)
Q_DECLARE_METATYPE(jlib::qt::ThreadProgress)
#pragma once
#include <QDebug>
#ifdef _WIN32
#include <Windows.h>
#endif //_WIN32
#define QDEBUG_FILE_LINE_STREAM "[" << __FUNCTION__ << "ln" << __LINE__ << "]: "
#define QDEBUG_FILE_LINE_VER (QString("[") + __FUNCTION__ + " ln" + QString::number(__LINE__) + "]: ")
#define MYQDEBUG qDebug() << QDEBUG_FILE_LINE_STREAM
#define MYQWARN qWarning() << QDEBUG_FILE_LINE_STREAM
#define MYQCRITICAL qCritical() << QDEBUG_FILE_LINE_STREAM
\ No newline at end of file
#pragma once
// if not defined, disable redirect
// if defined, you must include log2.h before include this file
#define REDIRECT_QT_OUTPUT_TO_LOG
#ifdef REDIRECT_QT_OUTPUT_TO_LOG
#ifndef JLIB_LOG2_ENABLED
#error 'You must include <jlib/log2.h> first!'
#else // has JLIB_LOG2_ENABLED
static inline void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
using namespace jlib;
QByteArray localMsg = msg.toLocal8Bit();
switch (type) {
case QtDebugMsg:
JLOG_INFO(localMsg.data());
break;
case QtWarningMsg:
if (!msg.contains("libpng")) {
JLOG_WARN(localMsg.data());
}
break;
case QtCriticalMsg:
JLOG_CRTC(localMsg.data());
break;
case QtFatalMsg:
JLOG_ERRO(localMsg.data());
break;
case QtInfoMsg:
JLOG_INFO(localMsg.data());
break;
default:
JLOG_WARN(localMsg.data());
break;
}
}
#endif // JLIB_LOG2_ENABLED
#endif // REDIRECT_QT_OUTPUT_TO_LOG
#pragma once
#include <QDir>
#include <QCoreApplication>
#include <QString>
#include <QDateTime>
namespace jlib {
namespace qt {
static const auto FILE_TIME_FORMAT = "yyyy-MM-dd_hh-mm-ss";
struct PathHelper
{
static inline bool mkpath(const QString& path) {
QDir dir;
return dir.exists(path) ? true : dir.mkpath(path);
}
static inline QString program() {
struct Helper {
Helper() {
QDir dir(QCoreApplication::applicationDirPath());
dir.cdUp();
path = dir.absolutePath();
}
QString path = {};
};
static Helper helper;
return helper.path;
}
static inline QString bin() {
return program() + "/bin";
}
static inline QString log() {
return program() + "/log";
}
static inline QString sdk() {
return program() + "/sdks";
}
static inline QString data() {
return program() + "/data";
}
static inline QString config() {
return data() + "/config";
}
static inline QString db() {
return data() + "/db";
}
static inline QString pic() {
return data() + "/pic";
}
static inline QString rec() {
return data() + "/rec";
}
static inline QString currentTimeString() {
return QDateTime::currentDateTime().toString(FILE_TIME_FORMAT);
}
};
struct AutoSwithToBin {
AutoSwithToBin() {}
AutoSwithToBin(const QString& path) {
QDir().setCurrent(path);
}
~AutoSwithToBin() {
QDir().setCurrent(PathHelper::bin());
}
};
} // end of namespace qt
} // end of namespace jlib
#pragma once
#include <QString>
#include <QColor>
namespace jlib {
namespace qt {
namespace def_sizes {
static const int window_width = 646;
static const int window_height = 424;
static const int title_height = 54;
static const int content_height = window_height - title_height;
static const int control_height = 24;
static const int big_window_width = 1022;
static const int big_window_height = 670;
static const int big_content_height = big_window_height - title_height;
static const int device_view_width = 306;
static const int device_view_height = 236;
} // namespace def_sizes
namespace def_colors {
// 0x202224
static const auto window_bk = QColor(0x202224);
// 0x2c2d30
static const auto control_bk = QColor(0x2c2d30);
// 0x26272a
static const auto control_bk2 = QColor(0x26272a);
// 0x313235
static const auto control_bk_suspend = QColor(0x313235);
// 0x424446
static const auto control_bk_unknown = QColor(0x424446);
// 0xd7d5d5
static const auto unknown_trans = QColor(0xd7d5d5);
// 0xfd8b30
static const auto control_text_font = QColor(0xfd8b30);
// 0xb0afaf
static const auto icon_text_color = QColor(0xb0afaf);
// 0xfe3e3e
static const auto hint_color = QColor(0xfe3e3e);
// 0xa8a9a9
static const auto edit_color = QColor(0xa8a9a9);
// 0x626466
static const auto bg_normal = QColor(0x626466);
// 0x727476
static const auto bg_suspend = QColor(0x727476);
// 0xbbbbbb
static const auto font_normal = QColor(0xbbbbbb);
// 0xf1f1f1
static const auto font_suspend = QColor(0xf1f1f1);
// 0x838383
static const auto font_normal2 = QColor(0x838383);
// 0xaaaaaa
static const auto font_normal3 = QColor(0xaaaaaa);
// 0x929292
static const auto font_normal4 = QColor(0x929292);
// 0x1a1a1a
static const auto ptz_border = QColor(0x1a1a1a);
// 0x2d2e31
static const auto ptz_back = QColor(0x2d2e31);
} // namespace def_colors
namespace def_font_families {
static const char* const yahei = "Microsoft YaHei";
} // namespace def_font_families
inline QString color_value_to_string(int value) {
auto s = QString::number(value, 16);
if (s.length() == 1) { s += "0"; }
return s;
}
inline QString color_to_string_16(QColor color) {
return color_value_to_string(color.red()) + color_value_to_string(color.green()) + color_value_to_string(color.blue());
}
// background:[style];
inline QString build_bg_style(QString style = "transparent") {
return QString("background:") + style + QString(";");
}
// color:#[color];
inline QString build_color_style(QColor color) {
return QString("color:#") + color_to_string_16(color) + QString(";");
}
// background-color:#[color];
inline QString build_bgcolor_style(QColor bgcolor) {
return QString("background-color:#") + color_to_string_16(bgcolor) + QString(";");
}
// background-color:rgba([r],[g],[b],[a]%);
inline QString build_bgcolor_style(int r, int g, int b, int a) {
return QString("background-color:rgba(%1,%2,%3,%4%);").arg(r).arg(g).arg(b).arg(a);
}
// border:[border]px;
inline QString build_border_style(size_t border) {
return QString("border:") + QString::number(border) + QString("px;");
}
// font:[font_size]pt;font-family:[font_family];
inline QString bulid_font_style(size_t font_size, QString font_family = def_font_families::yahei) {
return QString("font:") + QString::number(font_size) + QString("pt;")
+ QString("font-family:") + font_family + QString(";");
}
// color:#[color];font:[font_size]pt;font-family:[font_family];
inline QString build_style(QColor color, size_t font_size, QString font_family = def_font_families::yahei) {
return build_color_style(color) + bulid_font_style(font_size, font_family);
}
// background-color:#[color];color:#[color];font:[font_size]pt;font-family:[font_family];[border:[border]px;]
inline QString build_style(QColor bgcolor, QColor color, size_t font_size,
QString font_family = def_font_families::yahei,
bool with_border = false, size_t border = 0) {
auto style = build_bgcolor_style(bgcolor) + build_style(color, font_size, font_family);
if (with_border) { style += build_border_style(border); }
return style;
}
// background-color:rgba([r],[g],[b],[a]%);color:#[color];font:[font_size]pt;font-family:[font_family];
inline QString build_style(int r, int g, int b, int a, QColor color, size_t font_size,
QString font_family = def_font_families::yahei) {
return build_bgcolor_style(r, g, b, a) + build_style(color, font_size, font_family);
}
namespace def_style_sheets {
// color:#ffffff;font:11pt;font-family:Microsoft YaHei;
inline QString label() {
return build_style(Qt::white, 11);
}
// color:#fe3e3e;font:9pt;font-family:Microsoft YaHei;
inline QString hint() {
return build_style(def_colors::hint_color, 9);
}
// color:#202224;font:12pt;font-family:Microsoft YaHei;border:0px;
inline QString edit() {
return build_style(def_colors::window_bk, def_colors::edit_color, 12, def_font_families::yahei, true);
}
// color:#424446;font:11pt;font-family:Microsoft YaHei;border:0px;
inline QString unknown() {
return build_style(def_colors::control_bk_unknown, def_colors::edit_color, 11, def_font_families::yahei, true);
}
// background:transparent;color:#d7d5d5;font:18pt;font-family:Microsoft YaHei;
inline QString unkonwn_trans() {
return build_bg_style() + build_style(def_colors::unknown_trans, 18);
}
static const auto vertical_scroll_bar = R"(
QScrollBar:vertical
{
width:8px;
background:rgba(0,0,0,0%);
margin:0px,0px,0px,0px;
padding-top:0px;
padding-bottom:0px;
}
QScrollBar::handle:vertical
{
width:8px;
background:#444547;
border-radius:4px;
min-height:20;
}
QScrollBar::handle:vertical:hover
{
width:8px;
background:#444547;
border-radius:4px;
min-height:20;
}
QScrollBar::add-line:vertical
{
height:9px;
width:8px;
border-image:url(:/images/a/3.png);
subcontrol-position:bottom;
}
QScrollBar::sub-line:vertical
{
height:9px;
width:8px;
border-image:url(:/images/a/1.png);
subcontrol-position:top;
}
QScrollBar::add-line:vertical:hover
{
height:9px;
width:8px;
border-image:url(:/images/a/4.png);
subcontrol-position:bottom;
}
QScrollBar::sub-line:vertical:hover
{
height:9px;
width:8px;
border-image:url(:/images/2.png);
subcontrol-positino:top;
}
QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical
{
background:rgba(0,0,0,10%);
border-radius:4px;
}
)";
static const auto search_bar_edit = R"(
border: 0px;
background: transparent;
selection-background-color: white;
color: #a8a9a9;
font-family: Microsoft Yahei;
font-size: 14px;
)";
static const auto progress_bar = R"(
QProgressBar{
background-color:#dedede;border-radius:3px;
}
QProgressBar::chunk{
background-color:#fd8b30;border-radius:3px;
}
)";
static const auto slider = R"(
QSlider::groove:horizontal
{
border:0px;
height:4px;
background:rgb(0,0,0);
}
QSlider::sub-page:horizontal
{
background:#fd8b30;
}
QSlider::add-page:horizontal
{
background:#141414;
}
QSlider::handle:horizontal
{
background:white;
width:10px;
border-radius:5px;
margin:-3px 0px -3px 0px;
}
)";
static const auto menu = R"(
QMenu::item
{
font-family: Microsoft Yahei;
font-size: 12pt;
color: #808080;
border: 1px solid gray;
background-color: rgb(234,234,234);
}
QMenu::item:hover
{
background-color: rgb(0, 0, 255);
}
)";
static const auto group_box = R"(
QGroupBox {
border: 1px solid gray;
border-radius: 9px;
margin-top: 0.5em;
font: 24pt;
font-family: Microsoft Yahei;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 20px;
padding: 0 3px 0 3px;
color: white;
}
)";
static const auto listWidgetStyleSheet = R"(
QListWidget
{
font:12pt;
font-family:Microsoft Yahei;
background-color: #2c2d30;
border: 1px solid black;
}
QListWidget::item
{
color:#fd8b30;
border: 1px solid silver;
}
QListWidget::item::selected
{
color:#fd8b30;
border: 3px solid yellow;
}
)";
static const auto check_box = R"(
QCheckBox
{
font-size: 16px;
color: white;
}
QCheckBox::indicator
{
width: 48px;
height: 48px;
}
QCheckBox::indicator:checked
{
image: url(:/Skin/checkbox/checkbox_checked.png);
}
QCheckBox::indicator:unchecked
{
image: url(:/Skin/checkbox/checkbox_unchecked.png);
}
)";
} // end of namespace def_style_sheets
} // end of namespace qt
} // end of namespace jlib
#pragma once
#include <cassert>
#include <QString>
#include <QColor>
#include <QPixmap>
#include <QApplication>
#include <QDesktopwidget>
#include <QIcon>
#include <QTimer>
#include <QDir>
#include <QProcess>
#include "QtDebug.h"
#include "QtPathHelper.h"
namespace jlib {
namespace qt {
/**
* @brief 不阻塞UI响应的情况下,等待一段时间
* @param ms 等待的毫秒数
*/
static inline void non_blocking_wait_in_ui_thread(int ms) {
QEventLoop q;
QTimer t;
t.setSingleShot(true);
QObject::connect(&t, &QTimer::timeout, &q, &QEventLoop::quit);
t.start(ms);
q.exec();
}
/**
* @brief 在文件夹中显示
* @param pathIn 若为文件路径则在文件夹中显示,若为文件夹路径则打开改文件夹
*/
static inline void showInGraphicalShell(const QString &pathIn) {
QString param;
if (!QFileInfo(pathIn).isDir())
param = QLatin1String("/select,");
param += QDir::toNativeSeparators(pathIn);
QString command = "explorer.exe " + param;
QProcess::startDetached(command);
}
static inline bool warn_if_load_pixmap_failed(QPixmap& pixmap, QString icon_path, QString file_line)
{
if (!QDir().isAbsolutePath(icon_path)) {
icon_path = PathHelper::program() + "/" + icon_path;
}
if (!pixmap.load(icon_path) && !pixmap.load(icon_path, "png")) {
qCritical() << file_line << "load pixmap failed: " << icon_path;
assert(0);
return false;
}
return true;
}
#define LOAD_PIXMAP_EX(icon_path) jlib::qt::warn_if_load_pixmap_failed(pixmap, icon_path, QDEBUG_FILE_LINE_VER)
static QIcon icon_from_path(QString path, QSize icon_sz) {
QPixmap pixmap;
LOAD_PIXMAP_EX(path);
return QIcon(pixmap.scaled(icon_sz, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
static inline void fill_bg_with_color(QWidget * widget, QColor color)
{
if (!widget) { return; }
widget->setAutoFillBackground(true);
QPalette palette;
palette.setColor(QPalette::Background, color);
widget->setPalette(palette);
}
static inline void set_pos(QWidget * widget, QRect pos)
{
if (!widget) { return; }
widget->setFixedSize(pos.width(), pos.height());
widget->move(pos.left(), pos.top());
}
static inline void set_image_bg(QWidget* widget, QPixmap pixmap)
{
if (!widget || pixmap.isNull())return;
QPalette palette;
palette.setBrush(QPalette::Window, QBrush(pixmap));
widget->setPalette(palette);
}
static inline void set_image_bg(QWidget* widget, QString icon_path)
{
QPixmap pixmap;
if (!LOAD_PIXMAP_EX(icon_path)) { return; }
set_image_bg(widget, pixmap);
}
static inline void center_to_parent(QWidget* widget, QWidget* parent)
{
if (!widget || !parent) { return; }
auto center = parent->rect().center();
widget->move(center.x() - widget->width() / 2, center.y() - widget->height() / 2);
}
static inline void center_to_desktop(QWidget* widget)
{
if (!widget) { return; }
auto center = QApplication::desktop()->availableGeometry(widget).center();
widget->move(center.x() - widget->width() / 2, center.y() - widget->height() / 2);
}
static inline void center_to_desktop(QWidget* widget, int new_width, int new_height)
{
if (!widget) { return; }
auto center = QApplication::desktop()->availableGeometry(widget).center();
widget->setFixedSize(new_width, new_height);
widget->move(center.x() - new_width / 2, center.y() - new_height / 2);
widget->adjustSize();
}
}
}
#pragma once
#include <QPushButton>
#include "../QtStylesheet.h"
namespace jlib
{
namespace qt
{
class BgColorBtn : public QPushButton
{
Q_OBJECT
public:
BgColorBtn(QWidget *parent = nullptr)
: QPushButton(parent)
{
connect(this, &QPushButton::clicked, this, &BgColorBtn::slot_clicked);
}
virtual ~BgColorBtn() {}
void set_btn_attr(QColor bg_normal, QColor bg_suspend, QColor font_color, unsigned int font_sz) {
bg_normal_ = bg_normal;
bg_suspend_ = bg_suspend;
font_color_ = font_color;
font_sz_ = font_sz;
set_normal_attr();
}
void set_tag(int tag) { tag_ = tag; }
int tag() const { return tag_; }
signals:
void sig_clicked(int tag);
private slots:
void slot_clicked();
protected:
virtual void enterEvent(QEvent* e) override {
if (!isEnabled() || (parent() && !parentWidget()->isEnabled()))return;
set_mouse_enter_attr(); setCursor(QCursor(Qt::PointingHandCursor));
}
virtual void leaveEvent(QEvent* e) override {
if (!isEnabled() || (parent() && !parentWidget()->isEnabled()))return;
set_normal_attr(); setCursor(QCursor(Qt::ArrowCursor));
}
private:
void set_normal_attr() { set_attr(bg_normal_, font_color_, font_sz_); }
void set_mouse_enter_attr() { set_attr(bg_suspend_, font_color_, font_sz_); }
void set_attr(QColor bg_color, QColor font_color, unsigned int font_sz) {
setStyleSheet(build_style(bg_color, font_color, font_sz));
}
private:
QColor bg_normal_ = {};
QColor bg_suspend_ = {};
QColor font_color_ = {};
unsigned int font_sz_ = {};
int tag_ = -1;
};
inline void BgColorBtn::slot_clicked()
{
if (tag_ != -1) {
emit sig_clicked(tag_);
}
}
} // namespace qt
} // namespace jlib
#pragma once
#include <QDialog>
#include <QLabel>
#include <QElapsedTimer>
#include <QUrl>
#include <QNetworkRequest>
#include <QByteArray>
#include <qmovie.h>
#include <qnetworkaccessmanager.h>
#include <qnetworkreply.h>
#include <system_error>
#include "../QtPathHelper.h"
#include "../QtUtils.h"
#include "../QtStylesheet.h"
#include "HttpDlgErrorCode.h"
#include <jlib/3rdparty/json/jsoncpp/json.h>
namespace jlib
{
namespace qt
{
class HttpDlg : public QDialog
{
Q_OBJECT
public:
enum HttpDlgViewSize {
sz_big,
sz_small,
};
HttpDlg(QWidget *parent = nullptr, HttpDlgViewSize sz = sz_big, int timeOut = 10)
: QDialog(parent)
, sz_(sz)
, time_out_sec_(timeOut)
{
setWindowModality(Qt::WindowModal);
setWindowFlags(Qt::FramelessWindowHint);
if (!parent) { setAttribute(Qt::WA_TranslucentBackground); }
if (sz == sz_small) { setFixedSize(200, 200);
} else { setFixedSize(630, 637); }
label_ = new QLabel(this); label_->resize(width(), height()); label_->move(0, 0);
elapse_ = new QLabel(this); elapse_->resize(180, 80);
elapse_->move((width() - elapse_->width()) / 2, (height() - elapse_->height()) / 2);
elapse_->setStyleSheet(build_style(Qt::darkYellow, 64));
elapse_->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
//elapse_->hide();
parent ? center_to_parent(this, parent) : center_to_desktop(this);
mgr = new QNetworkAccessManager(this);
}
~HttpDlg() {}
void get(const QUrl& url) {
if (connection_) { disconnect(connection_); }
connection_ = connect(mgr, &QNetworkAccessManager::finished, this, &HttpDlg::onFinished);
reply_ = mgr->get(QNetworkRequest(url));
run();
}
void post(const QUrl& url) {
if (connection_) { disconnect(connection_); }
connection_ = connect(mgr, &QNetworkAccessManager::finished, this, &HttpDlg::onFinished);
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
reply_ = mgr->post(request, QByteArray());
run();
}
void post(const QNetworkRequest& request, const QByteArray& data) {
if (connection_) { disconnect(connection_); }
connection_ = connect(mgr, &QNetworkAccessManager::finished, this, &HttpDlg::onFinished);
reply_ = mgr->post(request, data);
run();
}
std::error_code get_result() const { return result_; }
Json::Value getRoot() const { return root_; }
bool getValue(const QString& name, Json::Value& value) {
if (root_.isMember(name.toStdString())) {
value = root_[name.toStdString()];
return true;
} return false;
}
bool getValue(const QString& name, QString& value) {
Json::Value val;
if (getValue(name, val)) {
value = QString::fromUtf8(val.asCString());
return true;
} return false;
}
bool getValue(const QString& name, int& value) {
Json::Value val;
if (getValue(name, val)) {
value = val.asInt();
return true;
} return false;
}
int getHttpStatusCode() const { return httpStatusCode_; }
QString getHttpReason() const { return httpReason_; }
#define break_if_parse_int_value_failed(json, name, default_value) \
int name = default_value; \
if (!JsoncppHelper::safelyGetIntValue(json, #name, name)) { \
ec = HttpDlgErrorCode::ParseJsonError; \
break; \
}
#define break_if_parse_string_value_failed(json, name, default_value) \
QString name = default_value; \
if (!JsoncppHelper::safelyGetStringValue(json, #name, name)) { \
ec = HttpDlgErrorCode::ParseJsonError; \
break; \
}
protected:
void run() {
auto path = PathHelper::program();
path += sz_ == sz_small ? "/Skin/gif/ajax-loader-small.gif" : "/Skin/gif/preloader.gif";
auto movie = new QMovie(path);
label_->setMovie(movie);
movie->start();
timer_.start();
auto p = parentWidget();
if (p) { p->setEnabled(false); }
startTimer(1000);
QDialog::exec();
if (p) { p->setEnabled(true); }
movie->deleteLater();
}
virtual void timerEvent(QTimerEvent * e) override {
MYQDEBUG << time_out_sec_;
if (--time_out_sec_ > 0) { elapse_->setText(QString::number(time_out_sec_)); }
else {
MYQCRITICAL << "timeout";
disconnect(connection_); result_ = HttpDlgErrorCode::NetworkError;
QDialog::reject();
}
}
private slots:
void onFinished(QNetworkReply* reply) {
do {
if (!reply) {
MYQCRITICAL << "no reply"; result_ = HttpDlgErrorCode::NetworkError;
break;
}
if (QNetworkReply::NoError != reply->error()) {
httpReason_ = reply->errorString(); MYQCRITICAL << httpReason_;
result_ = HttpDlgErrorCode::NetworkError;
break;
}
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (!statusCode.isValid()) { MYQDEBUG << "statusCode is not valid"; break; }
httpStatusCode_ = statusCode.toInt();
if (httpStatusCode_ != 200) {
result_ = HttpDlgErrorCode::NetworkError;
httpReason_ = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
MYQCRITICAL << httpStatusCode_ << httpReason_;
break;
}
auto res = reply->readAll();
Json::Reader reader; root_.clear();
if (!reader.parse(res.constData(), root_)) {
//result_ = HttpDlgErrorCode::ParseJsonError;
//break;
}
MYQDEBUG << reply->url() << "reply:\n" << root_.toStyledString().data();
} while (false);
QDialog::accept();
reply->deleteLater();
}
private:
std::error_code result_ = {};
int httpStatusCode_ = 0;
QString httpReason_ = {};
Json::Value root_ = {};
int time_out_sec_ = 10;
QNetworkAccessManager* mgr = {};
QNetworkReply* reply_ = {};
QMetaObject::Connection connection_ = {};
QLabel* label_ = {};
QLabel* elapse_ = {};
HttpDlgViewSize sz_ = sz_big;
QElapsedTimer timer_ = {};
};
}
}
#pragma once
#include <QString>
#include <QObject>
#include "../ErrorCode.h"
namespace jlib
{
namespace qt
{
enum class HttpDlgErrorCode {
NetworkError = 1,
ParseJsonError,
};
}
}
namespace std {
ENABLE_ENUM_AS_ERROR_CODE(jlib::qt::HttpDlgErrorCode);
}
namespace
{
class HttpDlgErrorCategory : public std::error_category
{
public:
const char* name() const noexcept override {
return "HttpClient";
}
std::string message(int ev) const override {
switch (static_cast<jlib::qt::HttpDlgErrorCode>(ev)) {
case jlib::qt::HttpDlgErrorCode::NetworkError:
return QObject::tr("Network Error").toLocal8Bit().toStdString();
break;
case jlib::qt::HttpDlgErrorCode::ParseJsonError:
return QObject::tr("Cannot Parse Json").toLocal8Bit().toStdString();
break;
default:
return QObject::tr("Unkown Error").toLocal8Bit().toStdString();
break;
}
}
};
static const HttpDlgErrorCategory theHttpDlgErrorCategory = {};
}
inline std::error_code std::make_error_code(jlib::qt::HttpDlgErrorCode ec)
{
return { static_cast<int>(ec), theHttpDlgErrorCategory };
}
\ No newline at end of file
#pragma once
#include <QLabel>
#include <QMouseEvent>
#include <QPixmap>
#include <QBitmap>
#include "../QtUtils.h"
namespace jlib
{
namespace qt
{
class IconBtn : public QLabel
{
Q_OBJECT
public:
enum IconStatus : uint32_t {
status_normal = 0x00000001,
status_hover = 0x00000002,
status_press = 0x00000004,
status_disable = 0x00000008,
status_ing = 0x00000010,
// 不规则按钮
type_mask = 0x00000020,
// 不重绘已绘制区域(原demo建议尽量不用)
type_opaque_paint = 0x00000040,
// 长按功能
type_long_press = 0x00000080,
status_default = status_normal | status_hover | status_press,
};
IconBtn(QWidget *parent, QString icon_path, uint32_t state_set = IconStatus::status_default)
: QLabel(parent)
, icon_path_(icon_path)
, state_set_(state_set)
{
setAttribute(Qt::WA_TranslucentBackground, true);
long_press_timer_ = new QTimer(this);
connect(long_press_timer_, &QTimer::timeout, this, &IconBtn::slot_long_press_timeout);
state_set_ |= IconStatus::status_normal;
set_icon_path(icon_path_);
}
void set_pos(const QPoint& pos) { move(pos); }
void set_pos(int x, int y) { set_pos(QPoint(x, y)); }
void set_icon_path(const QString& icon_path) { icon_path_ = icon_path; refresh_icon_status(); }
void set_enabled(bool enable) { is_enable_ = enable; refresh_icon_status(); }
void set_ing_status(bool is_ing) { is_ing_status_ = is_ing; refresh_icon_status(); }
bool is_ing_status() const { return is_ing_status_; }
protected:
virtual void enterEvent(QEvent*) override {
if (!is_enable_) { return; }
setCursor(QCursor(Qt::PointingHandCursor));
is_mouse_hover_ = true;
refresh_icon_status();
emit sig_mouse_enter();
}
virtual void leaveEvent(QEvent*) override {
setCursor(QCursor(Qt::ArrowCursor));
is_mouse_hover_ = false;
is_mouse_press_ = false;
refresh_icon_status();
emit sig_mouse_leave();
}
virtual void mousePressEvent(QMouseEvent*) override {
if (!is_enable_) { return; }
is_mouse_press_ = true;
refresh_icon_status();
if (state_set_ & type_long_press) {
if (long_press_timer_->isActive()) {
long_press_timer_->stop();
}
long_press_timer_->setSingleShot(true);
long_press_timer_->start(450);
}
}
virtual void mouseReleaseEvent(QMouseEvent* e) override {
if (!is_enable_) { return; }
if (long_press_timer_->isActive()) { long_press_timer_->stop(); }
bool is_long_press = is_long_press_;
is_long_press_ = is_mouse_press_ = false;
refresh_icon_status();
if (Qt::LeftButton == e->button() && rect().contains(e->pos())) {
is_long_press ? emit long_press_trigger(false) : emit clicked();
}
}
signals:
void clicked();
void long_press_trigger(bool is_press);
void sig_mouse_enter();
void sig_mouse_leave();
private slots:
void slot_long_press_timeout();
private:
unsigned long state_set_ = 0;
QString icon_path_ = {};
bool is_enable_ = true;
bool is_ing_status_ = false;
bool is_mouse_hover_ = false;
bool is_mouse_press_ = false;
QTimer* long_press_timer_ = {};
bool is_long_press_ = false;
void refresh_icon_status() {
QString icon_path;
if (!is_enable_) {
if (!(state_set_ & status_disable)) { return; }
icon_path = icon_path_ + "_d.png";
} else if (is_mouse_press_) {
if (!(state_set_ & status_press)) { return; }
icon_path = icon_path_ + "_h.png";
} else if (is_ing_status_) {
if (!(state_set_ & status_ing)) { return; }
icon_path = icon_path_ + "_ing.png";
} else if (is_mouse_hover_) {
if (!(state_set_ & status_hover)) { return; }
icon_path = icon_path_ + "_p.png";
} else {
if (!(state_set_&status_normal)) { return; }
icon_path = icon_path_ + "_n.png";
}
QPixmap pixmap;
if (!LOAD_PIXMAP_EX(icon_path)) { return; }
setFixedSize(pixmap.size()); setPixmap(pixmap);
if (state_set_ & type_mask) { setMask(pixmap.mask()); }
if (state_set_ & type_opaque_paint) { setAttribute(Qt::WA_OpaquePaintEvent, true); }
}
};
inline void IconBtn::slot_long_press_timeout()
{
is_long_press_ = true;
emit long_press_trigger(true);
}
}
}
#pragma once
#include <QDialog>
#include <qlabel.h>
#include "../Ctrl/ThreadCtrl.h"
#include "../Model/ThreadModel.h"
#include <vector>
#include <qelapsedtimer.h>
#include <qmovie.h>
#include "../QtUtils.h"
#include "../QtStylesheet.h"
#include "../QtPathHelper.h"
namespace jlib
{
namespace qt
{
class LoadingView : public QDialog
{
Q_OBJECT
public:
enum LoadingViewSize {
sz_big,
sz_small,
};
LoadingView(QWidget *parent = nullptr, std::vector<ThreadCtrl*> threads = {}, LoadingViewSize sz = sz_big)
: QDialog(parent)
, threads_(threads)
, sz_(sz)
{
setWindowModality(Qt::WindowModal);
setWindowFlags(Qt::FramelessWindowHint);
if (!parent) { setAttribute(Qt::WA_TranslucentBackground); }
if (sz == sz_small) { setFixedSize(200, 200);
} else { setFixedSize(630, 637); }
label_ = new QLabel(this); label_->resize(width(), height()); label_->move(0, 0);
progress1_ = new QLabel(this); progress1_->resize(180, 80);
progress2_ = new QLabel(this); progress2_->resize(180, 50);
elapse_ = new QLabel(this); elapse_->resize(180, 50);
progress1_->move((width() - progress1_->width()) / 2, (height() - progress1_->height() - progress2_->height() - elapse_->height()) / 2);
progress1_->setStyleSheet(build_style(Qt::green, 64));
progress1_->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
progress1_->hide();
progress2_->move(progress1_->x(), progress1_->y() + progress1_->height());
progress2_->setStyleSheet(build_style(Qt::blue, 32));
progress2_->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
progress2_->hide();
elapse_->move(progress1_->x(), progress2_->y() + progress2_->height());
elapse_->setStyleSheet(build_style(Qt::darkYellow, 48));
elapse_->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
elapse_->hide();
parent ? center_to_parent(this, parent) : center_to_desktop(this);
int tag = 0;
for (auto& thread_ : threads_) {
thread_->set_tag(tag++);
tp_.push_back(ThreadProgress(0, 1));
connect(thread_, SIGNAL(sig_ready(int, std::error_code)), this, SLOT(slot_ready(int, std::error_code)));
connect(thread_, SIGNAL(sig_progress(int, HBVideoPlatform::common::ThreadProgress)), this, SLOT(slot_progress(int, HBVideoPlatform::common::ThreadProgress)));
}
}
virtual ~LoadingView() {
for (auto thread_ : threads_) {
thread_->deleteLater();
}
}
void run() {
auto path = PathHelper::program();;
path += sz_ == sz_small ? "/Skin/gif/ajax-loader-small.gif" : "/Skin/gif/preloader.gif";
auto movie = new QMovie(path);
label_->setMovie(movie);
movie->start();
timer_.start();
for (auto thread_ : threads_) {
thread_->start();
}
auto p = parentWidget();
if (p) {
p->setEnabled(false);
}
if (!show_progress_) {
show_progress_ = true;
startTimer(1000);
}
QDialog::exec();
if (p) {
p->setEnabled(true);
}
}
std::error_code get_result() const { return result_; }
private slots:
void slot_ready(int tag, std::error_code ec);
void slot_progress(int tag, jlib::qt::ThreadProgress progress);
protected:
virtual void timerEvent(QTimerEvent* e) override {
if (show_progress_) {
int secs = timer_.elapsed() / 1000;
int mins = secs / 60;
secs %= 60;
elapse_->setText(QString().sprintf("%02d:%02d", mins, secs));
elapse_->show();
}
}
private:
std::error_code result_ = {};
std::vector<ThreadCtrl*> threads_ = {};
QLabel* label_ = {};
QLabel* progress1_ = {};
QLabel* progress2_ = {};
QLabel* elapse_ = {};
std::vector<ThreadProgress> tp_ = {};
bool show_progress_ = false;
LoadingViewSize sz_ = sz_big;
QElapsedTimer timer_ = {};
};
inline void LoadingView::slot_progress(int tag, jlib::qt::ThreadProgress progress)
{
tp_[tag].total = progress.total;
tp_[tag].pos = progress.pos;
int pos = 0; int total = 0;
for (auto tp : tp_) {
pos += tp.pos;
total += tp.total;
}
int percent = pos * 100 / total;
MYQDEBUG << "tag" << tag << "progress.pos" << progress.pos << "progress.total" << progress.total << "pos" << pos << "total" << total << "percent" << percent;
progress1_->setText(QString("%1%").arg(percent));
progress1_->show();
progress2_->setText(QString("%1/%2").arg(pos).arg(total));
progress2_->show();
int secs = timer_.elapsed() / 1000;
int mins = secs / 60;
secs %= 60;
elapse_->setText(QString().sprintf("%02d:%02d", mins, secs));
elapse_->show();
}
inline void LoadingView::slot_ready(int tag, std::error_code result)
{
MYQDEBUG << "tag" << tag << "ready, code=" << result.value() << "msg=" << QString::fromLocal8Bit(result.message().data());
result_ = result;
if (show_progress_) {
progress1_->setText("100%");
progress1_->show();
}
for (auto iter = threads_.begin(); iter != threads_.end(); iter++) {
auto thread = *iter;
if (thread->get_tag() == tag) {
threads_.erase(iter);
break;
}
}
if (!threads_.empty()) { return; }
QDialog::accept();
}
}
}
#pragma once
#include <QWidget>
#include <QPushButton>
#include <QToolButton>
#include <QLayout>
#include <QStyle>
#include <QLabel>
#include <QMouseEvent>
#include <QElapsedTimer>
#include <QLayout>
#include <QPixmap>
#include <QApplication>
#include "IconBtn.h"
#include "../QtStylesheet.h"
namespace jlib
{
namespace qt
{
class PageTitle : public QWidget
{
Q_OBJECT
public:
PageTitle(QWidget *parent, QString logo_path)
: QWidget(parent)
{
setAutoFillBackground(true);
QPalette pal = palette();
pal.setColor(QPalette::Window, Qt::black);
setPalette(pal);
minimize_ = new IconBtn(this, "Skin/system/min");
maximize_ = new IconBtn(this, "Skin/system/max");
close_ = new IconBtn(this, "Skin/system/close");
connect(minimize_, SIGNAL(clicked()), this, SLOT(slot_minimize()));
connect(maximize_, SIGNAL(clicked()), this, SLOT(slot_maximize_or_restore()));
connect(close_, SIGNAL(clicked()), this, SLOT(slot_close()));
logo_ = new QLabel(this);
QPixmap pixmap;
pixmap.load(logo_path);
logo_->setFixedSize(32, 32);
logo_->setPixmap(pixmap.scaled(32, 32, Qt::KeepAspectRatio));
title_ = new QLabel(this);
//title_->setStyleSheet(def_style_sheets::title);
title_->setStyleSheet(build_style(def_colors::font_normal3, 18));
layout_ = new QHBoxLayout(this);
layout_->addWidget(logo_);
layout_->addSpacing(5);
layout_->addWidget(title_);
layout_->addStretch();
layout_->addWidget(minimize_);
layout_->addWidget(maximize_);
layout_->addWidget(close_);
layout_->setSpacing(0);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setMinimumHeight(7);
lastTimeClick.start();
}
public:
void set_title(QString title) {
title_->setText(title);
}
void set_maximize_btn_visible(bool visible) {
visible ? maximize_->show() : maximize_->hide();
}
void set_maximized(bool isMax = true) {
is_maximized_ = isMax;
if (isMax) {
maximize_->set_icon_path("Skin/system/restore");
parentWidget()->showMaximized();
} else {
maximize_->set_icon_path("Skin/system/max");
parentWidget()->showNormal();
}
}
protected:
virtual void mousePressEvent(QMouseEvent* e) override {
if (!rect().contains(e->pos()))return;
is_mouse_pressed_ = true;
mouse_pressed_pos_ = mapToParent(e->pos());
}
virtual void mouseMoveEvent(QMouseEvent* e) override {
if (!is_mouse_pressed_)return;
if (is_maximized_) {
maximize_->set_icon_path("Skin/system/max");
parentWidget()->showNormal();
is_maximized_ = false;
}
parentWidget()->move(e->globalPos() - mouse_pressed_pos_);
}
virtual void mouseReleaseEvent(QMouseEvent* e) override {
is_mouse_pressed_ = false;
if (lastTimeClick.elapsed() < QApplication::doubleClickInterval()) {
slot_maximize_or_restore();
}
lastTimeClick.start();
}
signals:
void sig_close();
void sig_maximized(bool isMax);
private slots:
void slot_minimize();
void slot_maximize_or_restore();
void slot_close();
private:
IconBtn* minimize_ = {};
IconBtn* maximize_ = {};
IconBtn* close_ = {};
QLabel* logo_ = {};
QLabel* title_ = {};
QHBoxLayout* layout_ = {};
bool is_maximized_ = false;
bool is_mouse_pressed_ = false;
QPoint mouse_pressed_pos_ = {};
QElapsedTimer lastTimeClick = {};
};
inline void PageTitle::slot_minimize()
{
parentWidget()->showMinimized();
}
inline void PageTitle::slot_maximize_or_restore()
{
if (is_maximized_) {
maximize_->set_icon_path("Skin/system/max");
parentWidget()->showNormal();
is_maximized_ = false;
} else {
maximize_->set_icon_path("Skin/system/restore");
parentWidget()->showMaximized();
is_maximized_ = true;
}
emit sig_maximized(is_maximized_);
}
inline void PageTitle::slot_close()
{
emit sig_close();
}
}
}
#pragma once
#include <QtWidgets>
#include "../QtStylesheet.h"
namespace jlib
{
namespace qt
{
class RndButton : public QWidget
{
Q_OBJECT
public:
RndButton(QWidget *parent)
: QWidget(parent)
{
txt_ = new QLabel(this);
txt_->setAlignment(Qt::AlignCenter);
txt_->hide();
}
~RndButton() {}
void set_attr(QString txt, QSize btn_sz = { 60, 30 }, int font_size = 14) {
font_sz_ = font_size;
txt_->setStyleSheet(build_style(Qt::white, font_size));
txt_->setText(txt);
setFixedSize(btn_sz);
/*QPixmap pixmap;
LOAD_PIXMAP_EX(QString::fromLocal8Bit("Skin/Ӧÿ1.png"));
QSize pixSize = pixmap.size();
pixSize.scale(sz, Qt::KeepAspectRatio);
pixmap_ = pixmap.scaled(pixSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);*/
txt_->resize(size());
txt_->move(0, 0);
txt_->show();
bk_color_ = def_colors::control_bk;
update();
}
void set_highlight(bool on = true) {
is_highlighted_ = on;
bk_color_ = is_highlighted_ ? Qt::lightGray : def_colors::control_bk;
update();
}
protected:
virtual void paintEvent(QPaintEvent* e) override {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
QPainterPath path;
//int radius = std::min(rect().width() / 20, rect().height() / 20);
path.addRoundedRect(QRectF(0, 0, width(), height()), 10, 10);
QPen pen(Qt::black, 1);
painter.setPen(pen);
painter.fillPath(path, bk_color_);
painter.drawPath(path);
}
virtual void enterEvent(QEvent* e) override {
setCursor(QCursor(Qt::PointingHandCursor));
bk_color_ = Qt::darkGray;
update();
}
virtual void leaveEvent(QEvent* e) override {
setCursor(QCursor(Qt::ArrowCursor));
bk_color_ = is_highlighted_ ? Qt::lightGray : def_colors::control_bk;
update();
is_pressed_ = false;
}
virtual void mousePressEvent(QMouseEvent* e) override {
bk_color_ = def_colors::control_bk;
update();
is_pressed_ = true;
}
virtual void mouseReleaseEvent(QMouseEvent* e) override {
bk_color_ = Qt::darkGray;
update();
if (is_pressed_) {
emit clicked();
is_pressed_ = false;
}
}
signals:
void clicked();
private:
QLabel* txt_ = {};
QPixmap pixmap_ = {};
int font_sz_ = {};
QColor bk_color_ = {};
QColor txt_color_ = {};
bool is_pressed_ = false;
bool is_highlighted_ = false;
};
}
}
#pragma once
#include <QObject>
//#define DEFINE_SIGNAL(f) signals:void sig_##f; public void connect_to_sigslot_##f;
#define CONNECT_SIG_SLOT(f) connect(this, SIGNAL(sig_##f), obj, SLOT(slot_##f));
......@@ -106,22 +106,6 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\jlib\3rdparty\json\jsoncpp\json.h" />
<ClInclude Include="qt\ErrorCode.h" />
<ClInclude Include="qt\Model\ThreadModel.h" />
<ClInclude Include="qt\QtDebug.h" />
<ClInclude Include="qt\QtDebugOutput.h" />
<ClInclude Include="qt\QtPathHelper.h" />
<ClInclude Include="qt\QtStylesheet.h" />
<ClInclude Include="qt\QtUtils.h" />
<ClInclude Include="qt\signal_slot.h" />
<ClInclude Include="qt\View\HttpDlgErrorCode.h" />
<QtMoc Include="qt\View\RndButton.h" />
<QtMoc Include="qt\View\PageTitle.h" />
<QtMoc Include="qt\View\LoadingView.h" />
<QtMoc Include="qt\View\IconBtn.h" />
<QtMoc Include="qt\View\HttpDlg.h" />
<QtMoc Include="qt\View\BgColorBtn.h" />
<QtMoc Include="qt\Ctrl\ThreadCtrl.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">
......
......@@ -23,18 +23,6 @@
<Extensions>qrc;*</Extensions>
<ParseFiles>false</ParseFiles>
</Filter>
<Filter Include="jlib_qt">
<UniqueIdentifier>{b6cfb732-f3bc-497e-8e86-2992309b5057}</UniqueIdentifier>
</Filter>
<Filter Include="jlib_qt\View">
<UniqueIdentifier>{1562c0f6-b90a-4a18-8ab5-6daed249b6ca}</UniqueIdentifier>
</Filter>
<Filter Include="jlib_qt\Ctrl">
<UniqueIdentifier>{ad3bcdb3-03b6-4aa0-a0b8-731c85d61258}</UniqueIdentifier>
</Filter>
<Filter Include="jlib_qt\Model">
<UniqueIdentifier>{3e5f6680-de77-46c4-a80f-9a6ce7715eb7}</UniqueIdentifier>
</Filter>
<Filter Include="jsoncpp">
<UniqueIdentifier>{0ea00850-28a6-45f9-af68-6c656c3572f1}</UniqueIdentifier>
</Filter>
......@@ -60,27 +48,6 @@
<QtMoc Include="qt_test.h">
<Filter>Header Files</Filter>
</QtMoc>
<QtMoc Include="qt\Ctrl\ThreadCtrl.h">
<Filter>jlib_qt\Ctrl</Filter>
</QtMoc>
<QtMoc Include="qt\View\BgColorBtn.h">
<Filter>jlib_qt\View</Filter>
</QtMoc>
<QtMoc Include="qt\View\HttpDlg.h">
<Filter>jlib_qt\View</Filter>
</QtMoc>
<QtMoc Include="qt\View\IconBtn.h">
<Filter>jlib_qt\View</Filter>
</QtMoc>
<QtMoc Include="qt\View\LoadingView.h">
<Filter>jlib_qt\View</Filter>
</QtMoc>
<QtMoc Include="qt\View\PageTitle.h">
<Filter>jlib_qt\View</Filter>
</QtMoc>
<QtMoc Include="qt\View\RndButton.h">
<Filter>jlib_qt\View</Filter>
</QtMoc>
</ItemGroup>
<ItemGroup>
<QtUic Include="qt_test.ui">
......@@ -96,32 +63,5 @@
<ClInclude Include="..\..\jlib\3rdparty\json\jsoncpp\json.h">
<Filter>jsoncpp</Filter>
</ClInclude>
<ClInclude Include="qt\Model\ThreadModel.h">
<Filter>jlib_qt\Model</Filter>
</ClInclude>
<ClInclude Include="qt\View\HttpDlgErrorCode.h">
<Filter>jlib_qt\View</Filter>
</ClInclude>
<ClInclude Include="qt\ErrorCode.h">
<Filter>jlib_qt</Filter>
</ClInclude>
<ClInclude Include="qt\QtDebug.h">
<Filter>jlib_qt</Filter>
</ClInclude>
<ClInclude Include="qt\QtDebugOutput.h">
<Filter>jlib_qt</Filter>
</ClInclude>
<ClInclude Include="qt\QtPathHelper.h">
<Filter>jlib_qt</Filter>
</ClInclude>
<ClInclude Include="qt\QtStylesheet.h">
<Filter>jlib_qt</Filter>
</ClInclude>
<ClInclude Include="qt\QtUtils.h">
<Filter>jlib_qt</Filter>
</ClInclude>
<ClInclude Include="qt\signal_slot.h">
<Filter>jlib_qt</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
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