Debian Codegraph @

src/album/frame/toptoolbar.cpp

raw
1 /*
2 * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd.
3 *
4 * Author: ZhangYong <[email protected]>
5 *
6 * Maintainer: ZhangYong <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "toptoolbar.h"
22 #include "application.h"
23 #include "controller/configsetter.h"
24 #include "controller/signalmanager.h"
25 #include "controller/viewerthememanager.h"
26 #include "utils/baseutils.h"
27 //#include "utils/shortcut.h"
28 #include "ac-desktop-define.h"
29
30 #include <dwindowminbutton.h>
31 #include <dwindowmaxbutton.h>
32 #include <dwindowclosebutton.h>
33 #include <dwindowoptionbutton.h>
34 #include <dtitlebar.h>
35
36 #include <QDebug>
37 #include <QHBoxLayout>
38 #include <QPainter>
39 #include <QProcess>
40 #include <QResizeEvent>
41 #include <QShortcut>
42 #include <QStyleFactory>
43 #include <QImageReader>
44 #include <QLabel>
45 #include <QPainterPath>
46 #include <DFontSizeManager>
47 #include <DApplicationHelper>
48 DWIDGET_USE_NAMESPACE
49
50 namespace {
51
52 //const int TOP_TOOLBAR_HEIGHT = 50;
53 //const int ICON_MARGIN = 6;
54
55 const QColor DARK_TOP_BORDERCOLOR = QColor(255, 255, 255, 0);
56 const QColor LIGHT_TOP_BORDERCOLOR = QColor(255, 255, 255, 0);
57
58 const QColor DARK_BOTTOM_BORDERCOLOR = QColor(0, 0, 0, 51);
59 const QColor LIGHT_BOTTOM_BORDERCOLOR = QColor(0, 0, 0, 26);
60 } // namespace
61
62 TopToolbar::TopToolbar(bool manager, QWidget *parent)
63 : DBlurEffectWidget(parent)
64 {
65 m_manager = manager;
66 QPalette palette;
67 palette.setColor(QPalette::Background, QColor(0, 0, 0, 0)); // 最后一项为透明度
68 setPalette(palette);
69 initMenu();
70 initWidgets();
71 AC_SET_ACCESSIBLE_NAME(this, TOP_TOOL_BAR);
72 AC_SET_OBJECT_NAME(this, TOP_TOOL_BAR);
73
74 AC_SET_ACCESSIBLE_NAME(m_titlebar, TITLE_BAR);
75 AC_SET_OBJECT_NAME(m_titlebar, TITLE_BAR);
76 }
77
78 void TopToolbar::mouseDoubleClickEvent(QMouseEvent *e)
79 {
80 if (e->button() == Qt::LeftButton) {
81 if (window()->isMaximized())
82 window()->showNormal();
83 else if (! window()->isFullScreen()) // It would be normal state
84 window()->showMaximized();
85 }
86 DBlurEffectWidget::mouseDoubleClickEvent(e);
87 }
88
89 void TopToolbar::paintEvent(QPaintEvent *e)
90 {
91 Q_UNUSED(e);
92 QPainter p(this);
93 QPixmap pixmap(":/icons/deepin/builtin/frame/titlebar.svg");
94 const QPalette pal = QGuiApplication::palette();
95 QBrush bgColor = QBrush(pixmap.scaled(size().width(), 74));
96 QRectF bgRect;
97 bgRect.setSize(size());
98 QPainterPath pp;
99 pp.addRoundedRect(QRectF(bgRect.x(), bgRect.y(), bgRect.width(), 60), 0, 0);
100 p.fillPath(pp, bgColor);
101 }
102
103 void TopToolbar::initWidgets()
104 {
105 m_layout = new QHBoxLayout(this);
106 m_layout->setContentsMargins(0, 0, 0, 0);
107 m_layout->setSpacing(0);
108 m_titlebar = new DTitlebar(this);
109 m_titlebar->setWindowFlags(Qt::WindowMinMaxButtonsHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint);
110 m_titlebar->setMenu(m_menu);
111 DPalette pa;
112 pa.setColor(DPalette::WindowText, QColor(255, 255, 255, 255));
113 // QLabel *pLabel = new QLabel();
114 // pLabel->setFixedSize(33, 32);
115 // QIcon icon = QIcon::fromTheme("deepin-album");
116 // pLabel->setPixmap(icon.pixmap(QSize(30, 30)));
117 // m_titlebar->addWidget(pLabel, Qt::AlignLeft);
118 m_titlebar->setIcon(QIcon::fromTheme("deepin-album"));
119 QObject::connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &TopToolbar::onThemeTypeChanged);
120 m_titlebar->setTitle("");
121 m_titletxt = new DLabel;
122 m_titletxt->setText("");
123 m_titletxt->setObjectName("");
124 m_titletxt->setAccessibleName("");
125 DFontSizeManager::instance()->bind(m_titletxt, DFontSizeManager::T7/*,QFont::DemiBold*/);
126 DPalette p = DApplicationHelper::instance()->palette(m_titletxt);
127 pa.setBrush(DPalette::Text, p.color(DPalette::TextTitle));
128 m_titletxt->setPalette(pa);
129 m_titlebar->addWidget(m_titletxt, Qt::AlignCenter);
130 QPalette titleBarPA;
131 titleBarPA.setColor(QPalette::ButtonText, QColor(255, 255, 255, 204));
132 titleBarPA.setColor(QPalette::WindowText, QColor(255, 255, 255, 255));
133 m_titlebar->setPalette(titleBarPA);
134 m_titlebar->setBackgroundTransparent(true);
135 m_layout->addWidget(m_titlebar);
136 connect(dApp->signalM, &SignalManager::updateFileName, this, &TopToolbar::onUpdateFileName);
137 }
138
139 QString TopToolbar::geteElidedText(QFont font, QString str, int MaxWidth)
140 {
141 QFontMetrics fontWidth(font);
142 int width = fontWidth.horizontalAdvance(str);
143 if (width >= MaxWidth) {
144 str = fontWidth.elidedText(str, Qt::ElideRight, MaxWidth);
145 }
146 return str;
147 }
148
149 void TopToolbar::onThemeTypeChanged()
150 {
151 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
152 Q_UNUSED(themeType);
153 QPalette pa1;
154 pa1.setColor(QPalette::ButtonText, QColor(255, 255, 255, 204));
155 m_titlebar->setPalette(pa1);
156 }
157
158 void TopToolbar::onUpdateFileName(const QString &filename)
159 {
160 QString a = geteElidedText(DFontSizeManager::instance()->get(DFontSizeManager::T7), filename, width() - 500);
161 m_titletxt->setText(a);
162 m_titletxt->setObjectName(a);
163 m_titletxt->setAccessibleName(a);
164 connect(dApp->signalM, &SignalManager::resizeFileName, this, [ = ]() {
165 QString b = geteElidedText(DFontSizeManager::instance()->get(DFontSizeManager::T7), filename, width() - 500);
166 m_titletxt->setText(b);
167 m_titletxt->setObjectName(b);
168 m_titletxt->setAccessibleName(b);
169 });
170 }
171
172 void TopToolbar::initMenu()
173 {
174 m_menu = new DMenu(this);
175 m_menu->addSeparator();
176 }
177
178 //void TopToolbar::onHelp()
179 //{
180 // if (m_manualPro.isNull()) {
181 // const QString pro = "dman";
182 // const QStringList args("deepin-image-viewer");
183 // m_manualPro = new QProcess(this);
184 // connect(m_manualPro.data(), SIGNAL(finished(int)),
185 // m_manualPro.data(), SLOT(deleteLater()));
186 // m_manualPro->start(pro, args);
187 // }
188 //}
189
190 //void TopToolbar::onDeepColorMode()
191 //{
192 // if (dApp->viewerTheme->getCurrentTheme() == ViewerThemeManager::Dark) {
193 // dApp->viewerTheme->setCurrentTheme(
194 // ViewerThemeManager::Light);
195 // } else {
196 // dApp->viewerTheme->setCurrentTheme(
197 // ViewerThemeManager::Dark);
198 // }
199 //}