| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #include "vtoolbar.h"
- #include <QDebug>
- #include <QMouseEvent>
- #include <QCoreApplication>
- #include <QToolButton>
- using namespace vnotex;
- VToolBar::VToolBar(QWidget *p_parent)
- : QToolBar(p_parent),
- m_window(p_parent)
- {
- setupUI();
- m_window->installEventFilter(this);
- }
- VToolBar::VToolBar(const QString &p_title, QWidget *p_parent)
- : QToolBar(p_title, p_parent),
- m_window(p_parent)
- {
- setupUI();
- m_window->installEventFilter(this);
- }
- void VToolBar::setupUI()
- {
- }
- void VToolBar::mousePressEvent(QMouseEvent *p_event)
- {
- QToolBar::mousePressEvent(p_event);
- m_lastPos = p_event->pos();
- }
- void VToolBar::mouseDoubleClickEvent(QMouseEvent *p_event)
- {
- QToolBar::mouseDoubleClickEvent(p_event);
- m_ignoreNextMove = true;
- maximizeRestoreWindow();
- }
- void VToolBar::maximizeRestoreWindow()
- {
- m_window->isMaximized() ? m_window->showNormal() : m_window->showMaximized();
- }
- void VToolBar::mouseMoveEvent(QMouseEvent *p_event)
- {
- auto delta = p_event->pos() - m_lastPos;
- if (!m_ignoreNextMove && !m_lastPos.isNull() && (qAbs(delta.x()) > 10 || qAbs(delta.y()) > 10)) {
- if (m_window->isMaximized()) {
- m_window->showNormal();
- } else {
- m_window->move(p_event->globalPos() - m_lastPos);
- }
- }
- QToolBar::mouseMoveEvent(p_event);
- }
- void VToolBar::mouseReleaseEvent(QMouseEvent *p_event)
- {
- QToolBar::mouseReleaseEvent(p_event);
- m_ignoreNextMove = false;
- m_lastPos = QPoint();
- }
- void VToolBar::addTitleBarIcons(const QIcon &p_minimizeIcon,
- const QIcon &p_maximizeIcon,
- const QIcon &p_restoreIcon,
- const QIcon &p_closeIcon)
- {
- addSeparator();
- addAction(p_minimizeIcon, tr("Minimize"),
- this, [this]() {
- m_window->showMinimized();
- });
- m_maximizeIcon = p_maximizeIcon;
- m_restoreIcon = p_restoreIcon;
- m_maximizeAct = addAction(p_maximizeIcon, tr("Maximize"),
- this, [this]() {
- maximizeRestoreWindow();
- });
- {
- auto closeAct = addAction(p_closeIcon, tr("Close"),
- this, [this]() {
- m_window->close();
- });
- auto btn = static_cast<QToolButton *>(widgetForAction(closeAct));
- btn->setProperty("DangerousButton", true);
- }
- updateMaximizeAct();
- }
- bool VToolBar::eventFilter(QObject *p_obj, QEvent *p_event)
- {
- if (p_obj == m_window) {
- if (p_event->type() == QEvent::WindowStateChange) {
- updateMaximizeAct();
- }
- }
- return QToolBar::eventFilter(p_obj, p_event);
- }
- void VToolBar::updateMaximizeAct()
- {
- if (m_window->isMaximized()) {
- m_maximizeAct->setIcon(m_restoreIcon);
- m_maximizeAct->setText(tr("Restore Down"));
- } else {
- m_maximizeAct->setIcon(m_maximizeIcon);
- m_maximizeAct->setText(tr("Maximize"));
- }
- }
|