-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainwindow.cpp
135 lines (95 loc) · 3.32 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "mainwindow.h"
MainWindow::MainWindow(QWidget * parent) :
QWidget(parent),
sizeGrip(this),
button(this),
textLabel(this),
spacer(100, 100),
currentState(STATE_OFF),
currentShadowSize(0)
{
setLayout(&mainLayout);
mainLayout.setMargin(currentShadowSize);
mainLayout.setSpacing(0);
mainLayout.setRowStretch(1 , 1);
mainLayout.addWidget(&mainTitle, 0 , 0 , 1 , 1);
mainLayout.addWidget(&button);
textLabel.setStyleSheet("QLabel { color: rgb(204, 204, 204); font-style: bold; font-size: 16px; }");
QFont font("AlternateGotNo3D");
textLabel.setFont(font);
textLabel.setText("USE SLIDER TO SWITCH WINDOW STATES");
setAutoFillBackground(false);
setAttribute(Qt::WA_TranslucentBackground, true);
mainLayout.addWidget(&textLabel, 2, 0, 1, 1, Qt::AlignHCenter);
mainLayout.addItem(&spacer, 3, 0, 1, 1, Qt::AlignHCenter);
sizeGrip.hide();
mainTitle.hide();
connect(this, SIGNAL(WindowTitleChanged()), &mainTitle, SLOT(UpdateWindowTitle()) );
connect(&button, SIGNAL(clicked()), this, SLOT(changeStates()) );
QDesktopWidget desktop;
QRect geom = desktop.availableGeometry(desktop.primaryScreen());
resize(800, 600);
move(geom.width()/2 - width()/2, geom.height()/2 - height()/2);
setMinimumSize(400, 400);
setWindowTitle(tr("Button"));
}
void MainWindow::paintEvent(QPaintEvent * event)
{
Q_UNUSED(event);
QStyleOption options;
options.init(this);
QStylePainter p(this);
p.drawPrimitive(QStyle::PE_Widget, options);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(Qt::NoPen);
painter.setBrush(QBrush(QColor(255, 255, 255)));
painter.drawRect(currentShadowSize, currentShadowSize, width() - 2*currentShadowSize, height() - 2*currentShadowSize);
update();
}
void MainWindow::setWindowTitle( const QString & title)
{
QWidget :: setWindowTitle(title);
emit WindowTitleChanged();
}
void MainWindow::resizeEvent(QResizeEvent * event)
{
Q_UNUSED(event);
if(isMaximized() || currentState == STATE_OFF)
currentShadowSize = 0;
else
currentShadowSize = SHADOW_SIZE;
mainLayout.setMargin(currentShadowSize);
sizeGrip.move (width () - 30 - currentShadowSize, height () - 30 - currentShadowSize);
sizeGrip.resize ( 30, 30);
}
void MainWindow::changeStates()
{
int deltaWindowSize = 28;
int shadowCorrection = 2*SHADOW_SIZE;
switch (currentState)
{
case STATE_OFF:
currentState = STATE_ON;
mainTitle.show();
sizeGrip.show();
setWindowFlags(Qt::FramelessWindowHint);
resize(width() + shadowCorrection, height() + deltaWindowSize + shadowCorrection );
move(pos().x() - shadowCorrection/2, pos().y() - shadowCorrection/2);
update();
show();
break;
case STATE_ON:
currentState = STATE_OFF;
mainTitle.hide();
sizeGrip.hide();
Qt::WindowFlags flags = windowFlags() ;
flags &= ~Qt::FramelessWindowHint;
setWindowFlags(flags);
resize(width() - shadowCorrection, height() - deltaWindowSize - shadowCorrection);
move(pos().x() + shadowCorrection/2, pos().y() + shadowCorrection/2);
update();
show();
break;
}
}