| 1 |
/* |
| 2 |
* |
| 3 |
* |
| 4 |
* Copyright (C) 2023, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Sisheng He <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
#include "kbaseframe.h" |
| 23 |
|
| 24 |
#include "themeController.h" |
| 25 |
#include <QApplication> |
| 26 |
#include <QDBusReply> |
| 27 |
#include <QPainter> |
| 28 |
#include <QPainterPath> |
| 29 |
#include <QtDBus/QDBusInterface> |
| 30 |
|
| 31 |
namespace kdk |
| 32 |
{ |
| 33 |
class KBaseFramePrivate : public QObject |
| 34 |
{ |
| 35 |
Q_OBJECT |
| 36 |
Q_DECLARE_PUBLIC(KBaseFrame) |
| 37 |
public: |
| 38 |
KBaseFramePrivate(KBaseFrame *parent); |
| 39 |
|
| 40 |
protected: |
| 41 |
void paintEvent(QPaintEvent *e); |
| 42 |
|
| 43 |
private: |
| 44 |
KBaseFrame *q_ptr; |
| 45 |
|
| 46 |
KBaseFrame::BorderRadiusStyle m_RadiusType; |
| 47 |
bool m_IsTabletMode = false; |
| 48 |
QDBusInterface *m_pStatusSessionDbus = nullptr; |
| 49 |
bool m_HeightAdaptive = false; |
| 50 |
bool m_IsContainer = false; |
| 51 |
|
| 52 |
void autoAdjustFollowTabletMode(); |
| 53 |
|
| 54 |
private Q_SLOTS: |
| 55 |
void mode_change_signal_slots(bool b); |
| 56 |
}; |
| 57 |
|
| 58 |
KBaseFramePrivate::KBaseFramePrivate(KBaseFrame *parent) |
| 59 |
: q_ptr(parent) |
| 60 |
{ |
| 61 |
Q_Q(KBaseFrame); |
| 62 |
setParent(parent); |
| 63 |
q->setFrameShape(QFrame::Box); |
| 64 |
} |
| 65 |
|
| 66 |
void KBaseFramePrivate::autoAdjustFollowTabletMode() |
| 67 |
{ |
| 68 |
m_pStatusSessionDbus = new QDBusInterface("com.kylin.statusmanager.interface", |
| 69 |
"/", |
| 70 |
"com.kylin.statusmanager.interface", |
| 71 |
QDBusConnection::sessionBus(), this); |
| 72 |
if (m_pStatusSessionDbus->isValid()) { |
| 73 |
QDBusReply<bool> is_tabletmode = m_pStatusSessionDbus->call("get_current_tabletmode"); |
| 74 |
mode_change_signal_slots(is_tabletmode.isValid() ? is_tabletmode.value() : false); |
| 75 |
connect(m_pStatusSessionDbus, SIGNAL(mode_change_signal(bool)), this, SLOT(mode_change_signal_slots(bool))); |
| 76 |
} else { |
| 77 |
mode_change_signal_slots(false); |
| 78 |
// qWarning() << "Create com.kylin.statusmanager.interface Interface Failed When : " << QDBusConnection::systemBus().lastError(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
void KBaseFramePrivate::mode_change_signal_slots(bool b) |
| 83 |
{ |
| 84 |
Q_Q(KBaseFrame); |
| 85 |
if (m_HeightAdaptive) { |
| 86 |
if (b) { |
| 87 |
q->setMinimumSize(550, TABLET_HEIGHT); |
| 88 |
q->setMaximumSize(16777215, TABLET_HEIGHT); |
| 89 |
} else { |
| 90 |
q->setMinimumSize(550, PC_HEIGHT); |
| 91 |
q->setMaximumSize(16777215, PC_HEIGHT); |
| 92 |
} |
| 93 |
} |
| 94 |
m_IsTabletMode = b; |
| 95 |
Q_EMIT q->tabletModeChanged(b); |
| 96 |
} |
| 97 |
|
| 98 |
KBaseFrame::KBaseFrame(QWidget *parent, KBaseFrame::BorderRadiusStyle style, bool heightAdaptive) |
| 99 |
: QFrame(parent) |
| 100 |
, d_ptr(new KBaseFramePrivate(this)) |
| 101 |
{ |
| 102 |
Q_D(KBaseFrame); |
| 103 |
setObjectName("KBaseFrame"); |
| 104 |
|
| 105 |
d->m_RadiusType = style; |
| 106 |
d->m_HeightAdaptive = heightAdaptive; |
| 107 |
d->m_IsContainer = false; |
| 108 |
|
| 109 |
if (heightAdaptive) { |
| 110 |
d->autoAdjustFollowTabletMode(); |
| 111 |
} else { |
| 112 |
d->mode_change_signal_slots(false); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
KBaseFrame::~KBaseFrame() |
| 117 |
{ |
| 118 |
} |
| 119 |
|
| 120 |
void KBaseFrame::setRadiusType(BorderRadiusStyle style) |
| 121 |
{ |
| 122 |
Q_D(KBaseFrame); |
| 123 |
d->m_RadiusType = style; |
| 124 |
update(); |
| 125 |
} |
| 126 |
|
| 127 |
bool KBaseFrame::getTabletMode() |
| 128 |
{ |
| 129 |
Q_D(KBaseFrame); |
| 130 |
return d->m_IsTabletMode; |
| 131 |
} |
| 132 |
|
| 133 |
void KBaseFrame::setContainer(bool b) |
| 134 |
{ |
| 135 |
Q_D(KBaseFrame); |
| 136 |
d->m_IsContainer = b; |
| 137 |
if (b) { |
| 138 |
this->setFrameShape(QFrame::NoFrame); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
bool KBaseFrame::isContainer() |
| 143 |
{ |
| 144 |
Q_D(KBaseFrame); |
| 145 |
return d->m_IsContainer; |
| 146 |
} |
| 147 |
|
| 148 |
void KBaseFrame::paintEvent(QPaintEvent *e) |
| 149 |
{ |
| 150 |
Q_UNUSED(e); |
| 151 |
Q_D(KBaseFrame); |
| 152 |
QPainter painter(this); |
| 153 |
painter.setRenderHint(QPainter::Antialiasing); // 反锯齿; |
| 154 |
if (this->frameShape() != QFrame::NoFrame) { |
| 155 |
painter.setBrush(ThemeController::getCustomColorFromDT("base-active")); |
| 156 |
} |
| 157 |
painter.setPen(Qt::transparent); |
| 158 |
QRect rect = this->rect(); |
| 159 |
|
| 160 |
if (d->m_RadiusType == BorderRadiusStyle::None) { |
| 161 |
painter.drawRoundedRect(rect, 0, 0); |
| 162 |
} else { |
| 163 |
QPainterPath painterPath; |
| 164 |
painterPath.addRoundedRect(rect, RADIUS_VALUE, RADIUS_VALUE); |
| 165 |
|
| 166 |
painterPath.setFillRule(Qt::WindingFill); |
| 167 |
if (d->m_RadiusType == BorderRadiusStyle::Bottom) { |
| 168 |
painterPath.addRect(rect.width() - RADIUS_VALUE, 0, RADIUS_VALUE, RADIUS_VALUE); |
| 169 |
painterPath.addRect(0, 0, RADIUS_VALUE, RADIUS_VALUE); |
| 170 |
} else if (d->m_RadiusType == BorderRadiusStyle::Top) { |
| 171 |
painterPath.addRect(0, rect.height() - RADIUS_VALUE, RADIUS_VALUE, RADIUS_VALUE); |
| 172 |
painterPath.addRect(rect.width() - RADIUS_VALUE, rect.height() - RADIUS_VALUE, RADIUS_VALUE, RADIUS_VALUE); |
| 173 |
} |
| 174 |
painter.drawPath(painterPath); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
#include "kbaseframe.moc" |
| 180 |
#include "moc_kbaseframe.cpp" |