pineapple-files/mainwindow.cpp

121 lines
3.4 KiB
C++
Raw Normal View History

2020-09-23 01:42:09 +08:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "statusbarwidget.h"
2020-09-23 18:33:30 +08:00
#include "verticalpreviewwidget.h"
2020-09-23 01:42:09 +08:00
2020-09-24 17:47:56 +08:00
#include <QDesktopServices>
2020-09-23 01:42:09 +08:00
#include <QFileSystemModel>
2020-09-24 17:47:56 +08:00
#include <QUrl>
2020-09-23 01:42:09 +08:00
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_fsmodel(new QFileSystemModel)
2020-09-23 18:33:30 +08:00
, m_previewWidget(new VerticalPreviewWidget)
2020-09-23 01:42:09 +08:00
{
ui->setupUi(this);
ui->cdUpButton->setIcon(qApp->style()->standardIcon(QStyle::SP_FileDialogToParent));
m_fsmodel->setRootPath("");
2020-09-23 18:33:30 +08:00
m_previewWidget->setModel(m_fsmodel);
ui->columnView->setPreviewWidget(m_previewWidget);
2020-09-23 01:42:09 +08:00
ui->columnView->setModel(m_fsmodel);
ui->listView->setModel(m_fsmodel);
ui->treeView->setModel(m_fsmodel);
ui->treeTable->setModel(m_fsmodel);
ui->treeTable->setItemsExpandable(false);
ui->treeTable->setRootIsDecorated(false);
ui->treeView->hideColumn(1);
ui->treeView->hideColumn(2);
ui->treeView->hideColumn(3);
StatusbarWidget * sb = new StatusbarWidget();
ui->statusbar->addPermanentWidget(sb);
connect(sb, &StatusbarWidget::requestMode, this, [this](int mode){
ui->stackedWidget->setCurrentIndex(mode);
});
2020-09-24 17:47:56 +08:00
connect(ui->listView, &QAbstractItemView::doubleClicked, this, &MainWindow::gotoModelIndexOrOpenFile);
connect(ui->treeTable, &QAbstractItemView::doubleClicked, this, &MainWindow::gotoModelIndexOrOpenFile);
2020-09-23 01:42:09 +08:00
connect(ui->columnView, &QAbstractItemView::clicked, this, &MainWindow::viewGotoModelIndex);
2020-09-24 17:47:56 +08:00
connect(ui->columnView, &QAbstractItemView::doubleClicked, this, &MainWindow::gotoModelIndexOrOpenFile);
2020-09-23 18:33:30 +08:00
connect(ui->columnView, &QColumnView::updatePreviewWidget, m_previewWidget, &VerticalPreviewWidget::updatePreviewContent);
2020-09-23 01:42:09 +08:00
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::viewGotoModelIndex(const QModelIndex &index)
{
if (!m_fsmodel->isDir(index)) {
return;
}
ui->listView->setRootIndex(index);
ui->treeTable->setRootIndex(index);
QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::Current;
// if (columnClicked->selectionModel()->isSelected(index))
// flags |= QItemSelectionModel::Select;
ui->columnView->selectionModel()->setCurrentIndex(index, flags);
2020-09-24 17:47:56 +08:00
ui->addressEdit->setText(m_fsmodel->filePath(index));
}
void MainWindow::gotoModelIndexOrOpenFile(const QModelIndex &index)
{
if (!m_fsmodel->isDir(index)) {
QDesktopServices::openUrl(QUrl::fromLocalFile(m_fsmodel->filePath(index)));
} else {
viewGotoModelIndex(index);
}
}
bool MainWindow::gotoUrl(const QString &path)
{
if (!QFileInfo::exists(path)) {
return false;
}
const QModelIndex &idx = m_fsmodel->index(path);
if (idx.isValid()) {
viewGotoModelIndex(idx);
return true;
}
return false;
2020-09-23 01:42:09 +08:00
}
void MainWindow::on_treeView_clicked(const QModelIndex &index)
{
if (!m_fsmodel->isDir(index)) {
return;
}
ui->listView->setRootIndex(index);
ui->treeTable->setRootIndex(index);
ui->columnView->setRootIndex(index);
}
void MainWindow::on_cdUpButton_clicked()
{
const QModelIndex &idx = ui->listView->rootIndex();
if (!idx.parent().isValid()) {
return;
}
viewGotoModelIndex(idx.parent());
}
2020-09-24 17:47:56 +08:00
void MainWindow::on_addressEdit_returnPressed()
{
gotoUrl(ui->addressEdit->text());
}