-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
768 lines (637 loc) · 25.5 KB
/
mainwindow.cpp
File metadata and controls
768 lines (637 loc) · 25.5 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "scanner.h"
#include "sunburstwidget.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QHeaderView>
#include <QTimer>
#include <QVBoxLayout>
#include <QtCharts/QPieSeries>
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <algorithm>
#include <QDesktopServices>
#include <QUrl>
#include <QMenu>
#include <QClipboard>
#include <QFileInfo>
#include <QDateTime>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_scanner(nullptr)
, m_updateTimer(new QTimer(this))
, m_isScanning(false)
, m_sunburstWidget(nullptr)
{
ui->setupUi(this);
// Создаем SunburstWidget и заменяем им placeholder
QWidget *sunburstTab = ui->tabSunburst;
QVBoxLayout *layout = qobject_cast<QVBoxLayout*>(sunburstTab->layout());
if (layout) {
// Удаляем placeholder
QLayoutItem *item = layout->takeAt(0);
if (item && item->widget()) {
delete item->widget();
}
delete item;
// Создаем и добавляем SunburstWidget
m_sunburstWidget = new SunburstWidget(sunburstTab);
layout->addWidget(m_sunburstWidget);
}
setupConnections();
setWindowTitle("Анализатор дискового пространства");
resize(1200, 800);
// Настройка таблицы
ui->filesTable->setColumnCount(4);
ui->filesTable->setHorizontalHeaderLabels({"Имя файла", "Размер", "Путь", "Дата изменения"});
ui->filesTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
ui->filesTable->verticalHeader()->setDefaultSectionSize(20);
ui->filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->filesTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->filesTable->setSortingEnabled(true);
ui->filesTable->setContextMenuPolicy(Qt::CustomContextMenu);
ui->filesTable->setSelectionMode(QAbstractItemView::ExtendedSelection);
// Настройка сортировки по размеру по умолчанию (по убыванию)
ui->filesTable->sortByColumn(1, Qt::DescendingOrder);
// Таймер для обновления визуализаций
m_updateTimer->setInterval(1000);
connect(m_updateTimer, &QTimer::timeout, this, &MainWindow::updateVisualizations);
ui->pathEdit->setText(QDir::homePath());
// Инициализация QChartView
ui->chartView->setRenderHint(QPainter::Antialiasing);
}
MainWindow::~MainWindow()
{
if (m_scanner) {
m_scanner->stop();
m_scanner->deleteLater();
}
delete ui;
}
void MainWindow::setupConnections()
{
connect(ui->browseBtn, &QPushButton::clicked, this, &MainWindow::onBrowseClicked);
connect(ui->scanBtn, &QPushButton::clicked, this, &MainWindow::onScanClicked);
connect(ui->stopBtn, &QPushButton::clicked, this, &MainWindow::onStopClicked);
// Контекстное меню таблицы
connect(ui->filesTable, &QTableWidget::customContextMenuRequested,
this, &MainWindow::onFilesTableCustomContextMenuRequested);
// Двойной клик по строке таблицы
connect(ui->filesTable, &QTableWidget::doubleClicked, this, [this](const QModelIndex &index) {
if (index.isValid()) {
openSelectedFile();
}
});
// Подключаем сигналы SunburstWidget
if (m_sunburstWidget) {
connect(m_sunburstWidget, &SunburstWidget::itemClicked, this, &MainWindow::onSunburstItemClicked);
connect(m_sunburstWidget, &SunburstWidget::itemDoubleClicked, this, &MainWindow::onSunburstItemDoubleClicked);
}
}
void MainWindow::onBrowseClicked()
{
QString dir = QFileDialog::getExistingDirectory(
this,
"Выберите директорию для анализа",
ui->pathEdit->text()
);
if (!dir.isEmpty()) {
ui->pathEdit->setText(dir);
}
}
void MainWindow::onScanClicked()
{
if (m_isScanning) {
qDebug() << "Сканирование уже выполняется";
return;
}
QString path = ui->pathEdit->text().trimmed();
if (path.isEmpty() || !QDir(path).exists()) {
QMessageBox::warning(this, "Ошибка", "Укажите существующий путь для сканирования");
return;
}
qDebug() << "Начинаем сканирование:" << path;
// Очистка предыдущих результатов
ui->filesTable->setRowCount(0);
m_allFiles.clear();
if (m_sunburstWidget) {
m_sunburstWidget->clear();
}
// Создаем сканер
if (m_scanner) {
m_scanner->deleteLater();
}
m_scanner = new Scanner(path, this);
connect(m_scanner, &Scanner::progress, this, &MainWindow::onScannerProgress);
connect(m_scanner, &Scanner::fileFound, this, &MainWindow::onScannerFileFound);
connect(m_scanner, &Scanner::finished, this, &MainWindow::onScannerFinished);
connect(m_scanner, &Scanner::error, this, &MainWindow::onScannerError);
// Настройка UI
ui->scanBtn->setEnabled(false);
ui->stopBtn->setEnabled(true);
ui->progressBar->setVisible(true);
ui->progressBar->setRange(0, 100);
ui->progressBar->setValue(0);
ui->statusLabel->setText("Подсчет файлов...");
m_isScanning = true;
m_updateTimer->start();
// Запуск сканирования
qDebug() << "Запускаем сканер...";
m_scanner->start();
}
void MainWindow::onStopClicked()
{
if (m_scanner && m_isScanning) {
qDebug() << "Останавливаем сканирование...";
ui->statusLabel->setText("Остановка сканирования...");
ui->stopBtn->setEnabled(false);
m_scanner->stop();
m_isScanning = false;
ui->scanBtn->setEnabled(true);
ui->progressBar->setVisible(false);
}
}
void MainWindow::onScannerProgress(int percent, const QString &path, int filesCount, qint64 totalSize)
{
ui->progressBar->setValue(percent);
QString status = QString("Сканирование: %1% | Файлов: %2 | Размер: %3")
.arg(percent)
.arg(filesCount)
.arg(formatSize(totalSize));
if (!path.isEmpty() && path.length() < 50) {
QString fileName = QFileInfo(path).fileName();
if (!fileName.isEmpty()) {
status += " | " + fileName;
}
}
ui->statusLabel->setText(status);
// Обновляем заголовок окна
setWindowTitle(QString("Анализатор дискового пространства - %1%").arg(percent));
// Обновляем таблицу каждые 100 файлов
static int lastUpdateCount = 0;
if (filesCount - lastUpdateCount >= 100) {
if (m_rootItem) {
updateLargestFiles(m_rootItem);
}
lastUpdateCount = filesCount;
}
}
void MainWindow::onScannerFileFound(const QString &filePath, qint64 size)
{
static int fileCounter = 0;
fileCounter++;
// Обновляем UI каждые 500 файлов
if (fileCounter % 500 == 0) {
QApplication::processEvents();
}
}
void MainWindow::onScannerFinished(std::shared_ptr<FileItem> root)
{
qDebug() << "Сканирование завершено";
m_rootItem = root;
m_isScanning = false;
m_updateTimer->stop();
ui->progressBar->setVisible(false);
ui->scanBtn->setEnabled(true);
ui->stopBtn->setEnabled(false);
if (root) {
// Собираем все файлы в m_allFiles для быстрого доступа
m_allFiles.clear();
collectFiles(root, m_allFiles);
qint64 totalSize = root->totalSize();
ui->statusLabel->setText(
QString("Готово. Всего: %1 | Файлов: %2")
.arg(formatSize(totalSize))
.arg(m_allFiles.size())
);
qDebug() << "Обновляем визуализации...";
// Обновляем визуализации
updateChart(root);
updateSunburst(root);
updateLargestFiles(root);
} else {
ui->statusLabel->setText("Сканирование отменено");
}
setWindowTitle("Анализатор дискового пространства");
// Очищаем сканер
if (m_scanner) {
m_scanner->deleteLater();
m_scanner = nullptr;
}
qDebug() << "Обработка завершения сканирования окончена";
}
void MainWindow::onScannerError(const QString &message)
{
qDebug() << "Ошибка сканирования:" << message;
QMessageBox::critical(this, "Ошибка сканирования", message);
onStopClicked();
}
void MainWindow::updateVisualizations()
{
// Периодическое обновление визуализаций во время сканирования
if (m_rootItem && !m_isScanning) {
// Если сканирование завершено, обновляем все
updateChart(m_rootItem);
updateSunburst(m_rootItem);
updateLargestFiles(m_rootItem);
}
}
void MainWindow::updateChart(std::shared_ptr<FileItem> root)
{
if (!root || root->children().isEmpty()) {
// Создаем пустую диаграмму
auto chart = new QtCharts::QChart();
chart->setTitle("Распределение дискового пространства");
chart->legend()->hide();
auto series = new QtCharts::QPieSeries();
series->append("Нет данных", 1);
chart->addSeries(series);
ui->chartView->setChart(chart);
return;
}
// Создаем круговую диаграмму из дочерних элементов корня
auto chart = new QtCharts::QChart();
chart->setTitle("Распределение дискового пространства");
chart->legend()->setAlignment(Qt::AlignRight);
chart->setAnimationOptions(QtCharts::QChart::SeriesAnimations);
auto series = new QtCharts::QPieSeries();
// Берем топ-8 самых крупных элементов
auto children = root->children();
qint64 totalSize = root->totalSize();
if (totalSize == 0) {
series->append("Нет данных", 1);
chart->addSeries(series);
ui->chartView->setChart(chart);
return;
}
// Сортируем по размеру
std::sort(children.begin(), children.end(),
[](const auto &a, const auto &b) {
return a->totalSize() > b->totalSize();
});
int count = 0;
qint64 othersSize = 0;
for (const auto &child : children) {
if (count < 8 && child->totalSize() > 0) {
qreal percentage = (child->totalSize() * 100.0) / totalSize;
if (percentage >= 0.5) { // Показываем только если > 0.5%
auto slice = series->append(
QString("%1\n%2%")
.arg(child->name())
.arg(percentage, 0, 'f', 1),
child->totalSize()
);
slice->setLabelVisible(percentage > 2.0);
count++;
} else {
othersSize += child->totalSize();
}
} else {
othersSize += child->totalSize();
}
}
// Добавляем категорию "Другие"
if (othersSize > 0) {
qreal percentage = (othersSize * 100.0) / totalSize;
if (percentage > 0) {
auto slice = series->append(
QString("Другие\n%1%").arg(percentage, 0, 'f', 1),
othersSize
);
slice->setLabelVisible(percentage > 2.0);
}
}
if (series->count() == 0) {
series->append("Нет данных", 1);
}
chart->addSeries(series);
ui->chartView->setChart(chart);
}
void MainWindow::updateLargestFiles(std::shared_ptr<FileItem> root)
{
if (!root) return;
// Если файлы уже собраны в m_allFiles (в onScannerFinished), используем их
if (m_allFiles.isEmpty()) {
collectFiles(root, m_allFiles);
}
// Сортируем по размеру (по убыванию)
std::sort(m_allFiles.begin(), m_allFiles.end(),
[](const auto &a, const auto &b) {
return a->size() > b->size();
});
// Временно отключаем сортировку для корректного заполнения
ui->filesTable->setSortingEnabled(false);
// Отображаем топ-100
int count = qMin(100, m_allFiles.size());
ui->filesTable->setRowCount(count);
for (int i = 0; i < count; ++i) {
const auto &file = m_allFiles[i];
if (!file) {
qDebug() << "Пустой файл на позиции" << i;
continue;
}
// Колонка 0: Имя файла
QTableWidgetItem *nameItem = new QTableWidgetItem(
!file->name().isEmpty() ? file->name() : "Неизвестно"
);
ui->filesTable->setItem(i, 0, nameItem);
// Колонка 1: Размер (сохраняем сырой размер для сортировки)
QTableWidgetItem *sizeItem = new QTableWidgetItem(formatSize(file->size()));
sizeItem->setData(Qt::UserRole, file->size()); // Сохраняем числовое значение
ui->filesTable->setItem(i, 1, sizeItem);
// Колонка 2: Путь
QTableWidgetItem *pathItem = new QTableWidgetItem(
!file->path().isEmpty() ? file->path() : "-"
);
ui->filesTable->setItem(i, 2, pathItem);
// Колонка 3: Дата изменения
QString dateStr = "-";
if (file->modified().isValid()) {
dateStr = file->modified().toString("dd.MM.yyyy HH:mm");
}
QTableWidgetItem *dateItem = new QTableWidgetItem(dateStr);
dateItem->setData(Qt::UserRole, file->modified()); // Сохраняем дату для сортировки
ui->filesTable->setItem(i, 3, dateItem);
}
ui->filesTable->resizeColumnsToContents();
ui->filesTable->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
// Включаем сортировку обратно и сортируем по размеру (по убыванию)
ui->filesTable->setSortingEnabled(true);
ui->filesTable->sortByColumn(1, Qt::DescendingOrder);
}
void MainWindow::collectFiles(std::shared_ptr<FileItem> item, QList<std::shared_ptr<FileItem>> &files)
{
if (!item) return;
if (!item->isDirectory()) {
files.append(item);
return;
}
for (const auto &child : item->children()) {
collectFiles(child, files);
}
}
QString MainWindow::formatSize(qint64 bytes) const
{
constexpr qint64 KB = 1024;
constexpr qint64 MB = KB * 1024;
constexpr qint64 GB = MB * 1024;
constexpr qint64 TB = GB * 1024;
if (bytes == 0) return "0 Б";
if (bytes >= TB)
return QString("%1 TB").arg(bytes / static_cast<double>(TB), 0, 'f', 2);
else if (bytes >= GB)
return QString("%1 GB").arg(bytes / static_cast<double>(GB), 0, 'f', 2);
else if (bytes >= MB)
return QString("%1 MB").arg(bytes / static_cast<double>(MB), 0, 'f', 2);
else if (bytes >= KB)
return QString("%1 KB").arg(bytes / static_cast<double>(KB), 0, 'f', 2);
else
return QString("%1 Б").arg(bytes);
}
void MainWindow::onFilesTableCustomContextMenuRequested(const QPoint &pos)
{
QModelIndex index = ui->filesTable->indexAt(pos);
if (!index.isValid()) return;
QMenu contextMenu(this);
// Определяем количество выделенных файлов
QList<QTableWidgetItem*> selectedItems = ui->filesTable->selectedItems();
int selectedRows = 0;
if (!selectedItems.isEmpty()) {
QSet<int> rows;
for (auto item : selectedItems) {
rows.insert(item->row());
}
selectedRows = rows.size();
}
// Добавляем действия в меню
QAction *openAction = contextMenu.addAction(
selectedRows == 1 ? "Открыть файл" : "Открыть файлы",
this, &MainWindow::openSelectedFile
);
QAction *openLocationAction = contextMenu.addAction(
selectedRows == 1 ? "Открыть расположение файла" : "Открыть расположения файлов",
this, &MainWindow::openSelectedFileLocation
);
contextMenu.addSeparator();
QAction *propertiesAction = contextMenu.addAction(
selectedRows == 1 ? "Свойства файла" : "Свойства файлов",
this, &MainWindow::showFileProperties
);
contextMenu.addSeparator();
QAction *copyPathAction = contextMenu.addAction(
"Копировать путь",
this, &MainWindow::copyFilePath
);
QAction *copyNameAction = contextMenu.addAction(
"Копировать имя файла",
this, &MainWindow::copyFileName
);
// Показываем контекстное меню
contextMenu.exec(ui->filesTable->viewport()->mapToGlobal(pos));
}
QList<std::shared_ptr<FileItem>> MainWindow::getSelectedFiles() const
{
QList<std::shared_ptr<FileItem>> selectedFiles;
QModelIndexList selectedIndexes = ui->filesTable->selectionModel()->selectedRows(0);
for (const QModelIndex &index : selectedIndexes) {
int row = index.row();
if (row >= 0 && row < m_allFiles.size()) {
selectedFiles.append(m_allFiles[row]);
}
}
return selectedFiles;
}
std::shared_ptr<FileItem> MainWindow::getFirstSelectedFile() const
{
QModelIndexList selectedIndexes = ui->filesTable->selectionModel()->selectedRows(0);
if (!selectedIndexes.isEmpty()) {
int row = selectedIndexes.first().row();
if (row >= 0 && row < m_allFiles.size()) {
return m_allFiles[row];
}
}
return nullptr;
}
void MainWindow::openSelectedFile()
{
auto selectedFiles = getSelectedFiles();
for (const auto &file : selectedFiles) {
if (file && QFile::exists(file->path())) {
QUrl fileUrl = QUrl::fromLocalFile(file->path());
if (!QDesktopServices::openUrl(fileUrl)) {
QMessageBox::warning(this, "Ошибка",
QString("Не удалось открыть файл:\n%1").arg(file->path()));
}
} else {
QMessageBox::warning(this, "Ошибка",
QString("Файл не существует:\n%1").arg(file ? file->path() : ""));
}
}
}
void MainWindow::openSelectedFileLocation()
{
auto selectedFiles = getSelectedFiles();
// Для нескольких файлов открываем общую директорию первого файла
if (!selectedFiles.isEmpty()) {
QString dirPath;
if (selectedFiles.size() == 1) {
// Для одного файла - его директорию
dirPath = QFileInfo(selectedFiles.first()->path()).absolutePath();
} else {
// Для нескольких файлов - директорию первого файла
dirPath = QFileInfo(selectedFiles.first()->path()).absolutePath();
}
if (QDir(dirPath).exists()) {
QUrl dirUrl = QUrl::fromLocalFile(dirPath);
if (!QDesktopServices::openUrl(dirUrl)) {
QMessageBox::warning(this, "Ошибка",
QString("Не удалось открыть директорию:\n%1").arg(dirPath));
}
} else {
QMessageBox::warning(this, "Ошибка",
QString("Директория не существует:\n%1").arg(dirPath));
}
}
}
void MainWindow::showFileProperties()
{
auto selectedFiles = getSelectedFiles();
if (selectedFiles.isEmpty()) return;
if (selectedFiles.size() == 1) {
// Показываем детальные свойства для одного файла
auto file = selectedFiles.first();
QFileInfo fileInfo(file->path());
QString properties = QString(
"<b>Свойства файла:</b><br>"
"<br>"
"<b>Имя:</b> %1<br>"
"<b>Путь:</b> %2<br>"
"<b>Размер:</b> %3<br>"
"<b>Дата создания:</b> %4<br>"
"<b>Дата изменения:</b> %5<br>"
"<b>Дата последнего доступа:</b> %6<br>"
"<b>Расширение:</b> %7<br>"
"<b>Только для чтения:</b> %8<br>"
"<b>Скрытый:</b> %9"
).arg(
fileInfo.fileName(),
fileInfo.absoluteFilePath(),
formatSize(fileInfo.size()),
fileInfo.birthTime().toString("dd.MM.yyyy HH:mm:ss"),
fileInfo.lastModified().toString("dd.MM.yyyy HH:mm:ss"),
fileInfo.lastRead().toString("dd.MM.yyyy HH:mm:ss"),
fileInfo.suffix(),
fileInfo.isReadable() && !fileInfo.isWritable() ? "Да" : "Нет",
fileInfo.isHidden() ? "Да" : "Нет"
);
QMessageBox::information(this, "Свойства файла", properties);
} else {
// Показываем суммарную информацию для нескольких файлов
qint64 totalSize = 0;
QDateTime oldestDate = QDateTime::currentDateTime();
QDateTime newestDate = QDateTime::fromSecsSinceEpoch(0);
for (const auto &file : selectedFiles) {
QFileInfo fileInfo(file->path());
totalSize += fileInfo.size();
if (fileInfo.lastModified() < oldestDate) {
oldestDate = fileInfo.lastModified();
}
if (fileInfo.lastModified() > newestDate) {
newestDate = fileInfo.lastModified();
}
}
QString summary = QString(
"<b>Свойства выбранных файлов (%1 шт.):</b><br>"
"<br>"
"<b>Общий размер:</b> %2<br>"
"<b>Самый старый файл:</b> %3<br>"
"<b>Самый новый файл:</b> %4"
).arg(
QString::number(selectedFiles.size()),
formatSize(totalSize),
oldestDate.toString("dd.MM.yyyy HH:mm:ss"),
newestDate.toString("dd.MM.yyyy HH:mm:ss")
);
QMessageBox::information(this, "Свойства файлов", summary);
}
}
void MainWindow::copyFilePath()
{
auto selectedFiles = getSelectedFiles();
if (selectedFiles.isEmpty()) return;
QStringList paths;
for (const auto &file : selectedFiles) {
paths.append(file->path());
}
QString clipboardText = paths.join("\n");
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(clipboardText);
// Краткое уведомление
ui->statusLabel->setText(
QString("Скопировано %1 путь(ей)").arg(selectedFiles.size())
);
}
void MainWindow::copyFileName()
{
auto selectedFiles = getSelectedFiles();
if (selectedFiles.isEmpty()) return;
QStringList names;
for (const auto &file : selectedFiles) {
names.append(file->name());
}
QString clipboardText = names.join("\n");
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(clipboardText);
// Краткое уведомление
ui->statusLabel->setText(
QString("Скопировано %1 им(ён)").arg(selectedFiles.size())
);
}
void MainWindow::updateSunburst(std::shared_ptr<FileItem> root)
{
if (!m_sunburstWidget) return;
if (!root) {
m_sunburstWidget->clear();
return;
}
qDebug() << "Обновление Sunburst...";
m_sunburstWidget->setRootItem(root);
}
void MainWindow::onSunburstItemClicked(std::shared_ptr<FileItem> item)
{
if (!item) return;
ui->statusLabel->setText(
QString("Выбрано: %1 - %2")
.arg(item->name())
.arg(formatSize(item->totalSize()))
);
qDebug() << "Sunburst клик:" << item->name();
}
void MainWindow::onSunburstItemDoubleClicked(std::shared_ptr<FileItem> item)
{
if (!item) return;
qDebug() << "Sunburst двойной клик:" << item->name();
// Если это файл - открываем его
if (!item->isDirectory()) {
if (QFile::exists(item->path())) {
QUrl fileUrl = QUrl::fromLocalFile(item->path());
if (!QDesktopServices::openUrl(fileUrl)) {
QMessageBox::warning(this, "Ошибка",
QString("Не удалось открыть файл:\n%1").arg(item->path()));
}
} else {
QMessageBox::warning(this, "Ошибка",
QString("Файл не существует:\n%1").arg(item->path()));
}
} else {
// Если это директория - открываем ее в проводнике
QUrl dirUrl = QUrl::fromLocalFile(item->path());
if (!QDesktopServices::openUrl(dirUrl)) {
QMessageBox::warning(this, "Ошибка",
QString("Не удалось открыть директорию:\n%1").arg(item->path()));
}
}
}