-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
284 lines (227 loc) · 9.93 KB
/
Copy pathmainwindow.cpp
File metadata and controls
284 lines (227 loc) · 9.93 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileSystemModel>
#include <QListView>
#include <QTreeView>
#include <QIcon>
#include <QDir>
#include <QStandardPaths>
#include <QStack>
#include <QDesktopServices>
#include <QUrl>
#include <QClipboard>
#include <QApplication>
#include <QTimer>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QDebug>
#include <QSysInfo>
#include <QLabel>
// Custom QFileSystemModel to override folder icons
class CustomFileSystemModel : public QFileSystemModel {
public:
explicit CustomFileSystemModel(QObject *parent = nullptr) : QFileSystemModel(parent) {}
// Override data function to provide custom folder icons
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override {
if (role == Qt::DecorationRole && isDir(index)) {
return QIcon(":/Res/icons/empty_folder.png"); // Custom folder icon
}
return QFileSystemModel::data(index, role);
}
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, currentPath("") // Initialize currentPath
{
ui->setupUi(this);
// Initialize history stacks for navigation
backStack = new QStack<QString>();
forwardStack = new QStack<QString>();
// Create and set up the custom file system model
fileSystemModel = new CustomFileSystemModel(this);
fileSystemModel->setRootPath(QDir::rootPath()); // Set the model's root path
// Set up the QTreeView and QListView with the model
ui->treeView->setModel(fileSystemModel);
ui->listView->setModel(fileSystemModel);
// Configure QListView appearance
ui->listView->setViewMode(QListView::IconMode);
ui->listView->setIconSize(QSize(64, 64)); // Set icon size
ui->listView->setGridSize(QSize(100, 100)); // Set grid size
// Fetch and display system information in a scrollable area
QWidget *infoWidget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(infoWidget);
// Fetch and display various system information
layout->addWidget(new QLabel("Operating System: " + QSysInfo::prettyProductName(), this));
layout->addWidget(new QLabel("CPU Architecture: " + QSysInfo::currentCpuArchitecture(), this));
layout->addWidget(new QLabel("Kernel Type: " + QSysInfo::kernelType(), this));
layout->addWidget(new QLabel("Kernel Version: " + QSysInfo::kernelVersion(), this));
layout->addWidget(new QLabel("Current Host Name: " + QSysInfo::machineHostName(), this));
layout->addWidget(new QLabel("Product Type: " + QSysInfo::productType(), this));
layout->addWidget(new QLabel("Product Version: " + QSysInfo::productVersion(), this));
layout->addWidget(new QLabel("Machine Type: " + QSysInfo::currentCpuArchitecture(), this));
layout->addWidget(new QLabel("System Locale: " + QLocale::system().name(), this));
layout->addWidget(new QLabel("User Name: " + QDir::home().dirName(), this));
// Set layout for the infoWidget
infoWidget->setLayout(layout);
// Set the infoWidget in the scroll area and make it resizable
ui->scrollArea_system_info->setWidget(infoWidget);
ui->scrollArea_system_info->setWidgetResizable(true);
}
MainWindow::~MainWindow()
{
delete ui; // Clean up UI
}
void MainWindow::setupFileSystemView(const QString &path) {
ui->current_path->setText(path); // Display the current path
// Set the root directory for both views
ui->treeView->setRootIndex(fileSystemModel->index(path));
ui->listView->setRootIndex(fileSystemModel->index(path));
}
void MainWindow::on_home_clicked() {
navigateTo(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)); // Navigate to Home
}
void MainWindow::on_C_drive_clicked() {
navigateTo(QDir::rootPath()); // Navigate to C drive
}
void MainWindow::on_Doc_btn_clicked() {
navigateTo(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)); // Navigate to Documents
}
void MainWindow::on_images_btn_clicked() {
navigateTo(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)); // Navigate to Pictures
}
void MainWindow::on_Music_btn_clicked() {
navigateTo(QStandardPaths::writableLocation(QStandardPaths::MusicLocation)); // Navigate to Music
}
void MainWindow::on_Video_btn_clicked() {
navigateTo(QStandardPaths::writableLocation(QStandardPaths::MoviesLocation)); // Navigate to Videos
}
void MainWindow::on_Downlaod_btn_clicked() {
navigateTo(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)); // Navigate to Downloads
}
void MainWindow::navigateTo(const QString &path) {
if (!path.isEmpty()) {
// Push the current path to the back stack before navigating
if (!currentPath.isEmpty()) {
backStack->push(currentPath);
}
// Clear the forward stack when navigating to a new path
forwardStack->clear();
// Set the new path and update the views
currentPath = path;
setupFileSystemView(path);
}
}
void MainWindow::on_back_btn_clicked() {
if (!backStack->isEmpty()) {
// Move current path to forward stack
forwardStack->push(currentPath);
// Get the last path from the back stack
currentPath = backStack->pop();
// Update the view
setupFileSystemView(currentPath);
}
}
void MainWindow::on_forward_btn_clicked() {
if (!forwardStack->isEmpty()) {
// Move current path to back stack
backStack->push(currentPath);
// Get the next path from the forward stack
currentPath = forwardStack->pop();
// Update the view
setupFileSystemView(currentPath);
}
}
void MainWindow::on_listView_doubleClicked(const QModelIndex &index) {
if (!index.isValid()) return; // Ensure valid index
QString filePath = fileSystemModel->filePath(index);
// Check if it's a directory
if (fileSystemModel->isDir(index)) {
// Navigate to the directory
navigateTo(filePath);
} else {
// Open the file using the default application
if (currentPath != filePath) { // Prevent opening the same file twice
QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
}
}
}
void MainWindow::on_copy_path_btn_clicked()
{
// Get the current path from the label
QString current_path = ui->current_path->text();
// Get the application's clipboard
QClipboard *clipboard = QApplication::clipboard();
// Copy the text to the clipboard
clipboard->setText(current_path);
// Change button text to "copied!"
ui->copy_path_btn->setText("copied!");
// Create a QTimer to revert the text after 0.5 seconds (500 milliseconds)
QTimer::singleShot(500, this, [this]() {
ui->copy_path_btn->setText("Copy Path"); // Revert button text
});
}
void MainWindow::on_system_vol_btn_clicked()
{
// Reset the file system model and set the root path
fileSystemModel = new CustomFileSystemModel(this);
fileSystemModel->setRootPath(QDir::rootPath());
// Set up the QTreeView and QListView with the new model
ui->treeView->setModel(fileSystemModel);
ui->listView->setModel(fileSystemModel);
// Configure QListView appearance
ui->listView->setViewMode(QListView::IconMode);
ui->listView->setIconSize(QSize(64, 64)); // Set icon size
ui->listView->setGridSize(QSize(100, 100)); // Set grid size
}
void MainWindow::on_home_main_btn_clicked()
{
// Clear any existing items in the list widget
ui->listWidget->clear();
// Create a widget to hold the buttons
QWidget *buttonWidget = new QWidget(this);
QGridLayout *layout = new QGridLayout(buttonWidget); // Create a grid layout
// Create a mapping of standard folders to their corresponding QStandardPaths::StandardLocation
QMap<QString, QStandardPaths::StandardLocation> standardFolders; // Pair of folder name and standard location
standardFolders.insert("Desktop", QStandardPaths::DesktopLocation);
standardFolders.insert("Documents", QStandardPaths::DocumentsLocation);
standardFolders.insert("Downloads", QStandardPaths::DownloadLocation);
standardFolders.insert("Pictures", QStandardPaths::PicturesLocation);
standardFolders.insert("Music", QStandardPaths::MusicLocation);
standardFolders.insert("Videos", QStandardPaths::MoviesLocation);
int row = 0;
int col = 0;
// Add buttons to the grid layout
for (const auto &folder : standardFolders.keys()) {
const QString &folderName = folder;
QPushButton *button = new QPushButton(folderName);
button->setIcon(QIcon(":/Res/icons/" + folderName.toLower() + "_folder.png")); // Dynamically set the icon based on folder name
button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); // Keep button size fixed
// Connect the button's clicked signal to a slot to handle the action
connect(button, &QPushButton::clicked, this, [this, folderName, standardFolders]() {
// Navigate to the appropriate folder using the mapped value
navigateTo(QStandardPaths::writableLocation(standardFolders[folderName]));
});
layout->addWidget(button, row, col); // Add the button to the grid layout
col++; // Move to the next column
// Move to the next row if the column limit is reached (e.g., 3 columns)
if (col >= 3) {
col = 0;
row++;
}
}
// Set the layout for the button widget
buttonWidget->setLayout(layout);
// Create a QListWidgetItem to hold the button widget
QListWidgetItem *item = new QListWidgetItem();
item->setSizeHint(buttonWidget->sizeHint()); // Set size hint for the item
// Add the item to the list widget and set the button widget as its item widget
ui->listWidget->addItem(item);
ui->listWidget->setItemWidget(item, buttonWidget); // Set the button widget as the widget for the item
}
void MainWindow::on_info_svg_clicked()
{
//showing the about window to user
about_xfile = new About_XFile_Manager(this);
about_xfile->show();
}