-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
268 lines (232 loc) · 9.53 KB
/
Copy pathmainwindow.cpp
File metadata and controls
268 lines (232 loc) · 9.53 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "mainwindow.hh"
#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QFrame>
#include <QGridLayout>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMouseEvent>
#include <QClipboard>
#include <QImage>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QUrl>
#include <QMimeData>
#include <QFileInfo>
#include <QFile>
#include <QtSvg/QSvgRenderer>
#include <QPainter>
#include <sidebar.hh>
#include <QSplitter>
#include <mediadroparea.hh>
#include <borderwidget.hh>
#include "overlaywindow.hh"
void setupTitleBar(QFrame *titleBar, QWidget *parent);
QPushButton *createTitleBarButton(const QString &text, QWidget *parent);
QFrame *createGripBar(QWidget *parent);
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
setObjectName("MainWindow");
fullscreenOn = false;
setWindowFlags(Qt::FramelessWindowHint);
setWindowTitle("Custom Title Bar");
setStyleSheet("background-color: #282c34;");
QFrame *titleBar = new QFrame(this);
setupTitleBar(titleBar, this);
Sidebar *sidebar = new Sidebar(this);
QSplitter *splitter = new QSplitter(Qt::Horizontal, this);
splitter->addWidget(sidebar);
QWidget *contentWidget = new QWidget(splitter);
QVBoxLayout *contentLayout = new QVBoxLayout(contentWidget);
Explorer *explorer = new Explorer(this, contentWidget);
contentLayout->addWidget(explorer);
connect(sidebar, &Sidebar::imagesButtonClicked, explorer, &Explorer::onImagesButtonClicked);
connect(sidebar, &Sidebar::allFilesButtonClicked, explorer, &Explorer::onAllFilesButtonClicked);
connect(sidebar, &Sidebar::gifsButtonClicked, explorer, &Explorer::onGifsButtonClicked);
connect(sidebar, &Sidebar::textsButtonClicked, explorer, &Explorer::onTextsButtonClicked);
connect(sidebar, &Sidebar::videosButtonClicked, explorer, &Explorer::onVideosButtonClicked);
QStackedLayout *stackedLayout = new QStackedLayout();
stackedLayout->addWidget(splitter);
dropArea = new MediaDropArea(this);
dropArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
stackedLayout->addWidget(dropArea);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->setSpacing(0);
mainLayout->addWidget(titleBar);
mainLayout->addLayout(stackedLayout);
mainLayout->setContentsMargins(0, 0, 0, 0);
contentLayout->setContentsMargins(0, 0, 0, 0);
m_borderWidget = new BorderWidget(this);
m_borderWidget->setGeometry(0, 0, width(), height());
m_isDragging = false;
connect(explorer, &Explorer::targetFolderChanged, sidebar, &Sidebar::updateButtonStyles);
resize(800, 500);
dropArea->setGeometry(0, 0, width(), height());
QSizeGrip *sizeGrip = new QSizeGrip(this);
sizeGrip->setFixedSize(10, 10);
sizeGrip->setStyleSheet("background-color: rgba(105, 105, 105, 0);");
sizeGrip->move(width() - sizeGrip->width(), height() - sizeGrip->height());
sizeGrip->raise();
showMediaDropArea(true);
setAcceptDrops(true);
KeyListener *m_keyListener = new KeyListener(this);
connect(m_keyListener, &KeyListener::hotKeyPressed, this, &MainWindow::toggleFullscreen);
fullscreenOn = true;
overlayWindow = new OverlayWindow(this);
fullscreenOn = false;
}
MainWindow::~MainWindow() {
}
// Handles mouse press events to initiate window dragging.
void MainWindow::mousePressEvent(QMouseEvent *event) {
m_isDragging = false;
int right_side_limit = width() - 100;
if (event->button() == Qt::LeftButton && event->pos().y() < 50 && event->pos().x() < right_side_limit) {
if (isMaximized()) {
resetWindowGeometry();
} else {
m_dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
}
event->accept();
m_isDragging = true;
}
}
// Manages window movement during mouse drag.
void MainWindow::mouseMoveEvent(QMouseEvent *event) {
if (m_isDragging && event->buttons() & Qt::MouseButton::LeftButton) {
move(event->globalPosition().toPoint() - m_dragPosition);
event->accept();
}
}
// Handles mouse release events to end window dragging.
void MainWindow::mouseReleaseEvent(QMouseEvent *event) {
if (m_isDragging && event->button() == Qt::LeftButton) {
m_isDragging = false;
onDragFinished();
event->accept();
}
}
// Finalizes the dragging process.
void MainWindow::onDragFinished() {
m_isDragging = false;
}
// Manages window resize events, adjusting size grip and other widgets.
void MainWindow::resizeEvent(QResizeEvent* event) {
QSizeGrip *sizeGrip = findChild<QSizeGrip *>();
if (sizeGrip) {
sizeGrip->move(width() - sizeGrip->width(), height() - sizeGrip->height());
}
m_borderWidget->setGeometry(0, 0, width(), height());
dropArea->setGeometry(0, 0, width(), height());
QWidget::resizeEvent(event);
}
// Sets up the title bar with logo, buttons, and style.
void setupTitleBar(QFrame *titleBar, QWidget *parent) {
titleBar->setFixedHeight(50);
titleBar->setStyleSheet("background-color: #21252b; border-bottom: 3px solid #2c313a;");
QLabel *logoLabel = new QLabel(titleBar);
logoLabel->setPixmap(QPixmap(":/icons/logo.svg").scaled(150, 250, Qt::KeepAspectRatio, Qt::SmoothTransformation));
logoLabel->setStyleSheet("border: none;");
QPushButton *closeButton = createTitleBarButton(":/icons/window-close.svg", parent);
QObject::connect(closeButton, &QPushButton::clicked, parent, &QWidget::close);
QPushButton *minimizeButton = createTitleBarButton(":/icons/window-minimize.svg", parent);
QObject::connect(minimizeButton, &QPushButton::clicked, parent, &QWidget::showMinimized);
QPushButton *restoreButton = createTitleBarButton(parent->isMaximized() ? ":/icons/window-restore.svg" : ":/icons/window-maximize.svg", parent);
restoreButton->setObjectName("restoreButton");
QObject::connect(restoreButton, &QPushButton::clicked, parent, [parent, restoreButton]() {
MainWindow* mainWindow = static_cast<MainWindow*>(parent);
if (parent->isMaximized()) {
parent->showNormal();
restoreButton->setIcon(QIcon::fromTheme(":/icons/window-maximize.svg"));
} else {
mainWindow->storedNormalWindowWidth = mainWindow->width();
parent->showMaximized();
restoreButton->setIcon(QIcon::fromTheme(":/icons/window-restore.svg"));
}
});
QFrame *gripBar = createGripBar(parent);
QHBoxLayout *titleLayout = new QHBoxLayout(titleBar);
titleLayout->addWidget(logoLabel, 0, Qt::AlignLeft | Qt::AlignVCenter);
titleLayout->addWidget(gripBar, 1);
titleLayout->addWidget(minimizeButton, 0, Qt::AlignCenter);
titleLayout->addWidget(restoreButton, 0, Qt::AlignRight);
titleLayout->addWidget(closeButton, 0, Qt::AlignRight);
titleLayout->setContentsMargins(10, 0, 10, 0);
}
// Creates a custom button for the title bar with specified icon and styles.
QPushButton* createTitleBarButton(const QString& iconPath, QWidget* parent) {
QPushButton* button = new QPushButton(parent);
button->setFixedSize(32, 32);
const QString baseStyleSheet = "QPushButton { border: none; border-radius: 5px; }";
const QString hoverColor = iconPath == ":/icons/window-close.svg" ? "#ff5f56" : "#393d47";
const QString pressedColor = iconPath == ":/icons/window-close.svg" ? "#e81123" : "#2a2e37";
button->setStyleSheet(baseStyleSheet +
"QPushButton:hover { background-color: " + hoverColor + "; }"
"QPushButton:pressed { background-color: " + pressedColor + "; }");
QSvgRenderer renderer(iconPath);
QPixmap pixmap(32, 32);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
renderer.render(&painter);
button->setIcon(QIcon(pixmap));
return button;
}
// Creates a grip bar for window resizing.
QFrame *createGripBar(QWidget *parent) {
QFrame *gripBar = new QFrame(parent);
gripBar->setStyleSheet("background-color: #21252b;");
return gripBar;
}
// Toggles the window between fullscreen and normal state.
void MainWindow::toggleFullscreen() {
fullscreenOn = !fullscreenOn;
if (fullscreenOn) {
if (!overlayWindow) {
overlayWindow = new OverlayWindow(this);
}
overlayWindow->show();
} else {
if (overlayWindow) {
overlayWindow->close();
}
}
}
// Resets window geometry when exiting from maximized state.
void MainWindow::resetWindowGeometry() {
if (isMaximized()) {
showNormal();
QPushButton *restoreButton = findChild<QPushButton *>("restoreButton");
if (restoreButton) {
restoreButton->setIcon(QIcon::fromTheme(":/icons/window-maximize.svg"));
}
m_dragPosition = QPoint(storedNormalWindowWidth / 2, 14);
}
}
// Toggles the visibility of the media drop area.
void MainWindow::showMediaDropArea(bool visible) {
MediaDropArea *dropArea = findChild<MediaDropArea *>();
if (dropArea) {
dropArea->setVisible(visible);
if (visible) {
dropArea->raise();
}
}
}
// Handles drag enter events for enabling drag-and-drop functionality.
void MainWindow::dragEnterEvent(QDragEnterEvent *event) {
if (event->mimeData()->hasUrls()) {
dropArea->setAttribute(Qt::WA_TransparentForMouseEvents, false);
event->acceptProposedAction();
}
}
// Handles drag leave events in the application.
void MainWindow::dragLeaveEvent(QDragLeaveEvent *event) {
event->accept();
}
// Checks if the window is in fullscreen mode.
bool MainWindow::isFullscreenOn() const {
return fullscreenOn;
}