pineapple-files/mainwindow.cpp
2020-09-24 17:47:56 +08:00

121 lines
3.4 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "statusbarwidget.h"
#include "verticalpreviewwidget.h"
#include <QDesktopServices>
#include <QFileSystemModel>
#include <QUrl>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_fsmodel(new QFileSystemModel)
, m_previewWidget(new VerticalPreviewWidget)
{
ui->setupUi(this);
ui->cdUpButton->setIcon(qApp->style()->standardIcon(QStyle::SP_FileDialogToParent));
m_fsmodel->setRootPath("");
m_previewWidget->setModel(m_fsmodel);
ui->columnView->setPreviewWidget(m_previewWidget);
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);
});
connect(ui->listView, &QAbstractItemView::doubleClicked, this, &MainWindow::gotoModelIndexOrOpenFile);
connect(ui->treeTable, &QAbstractItemView::doubleClicked, this, &MainWindow::gotoModelIndexOrOpenFile);
connect(ui->columnView, &QAbstractItemView::clicked, this, &MainWindow::viewGotoModelIndex);
connect(ui->columnView, &QAbstractItemView::doubleClicked, this, &MainWindow::gotoModelIndexOrOpenFile);
connect(ui->columnView, &QColumnView::updatePreviewWidget, m_previewWidget, &VerticalPreviewWidget::updatePreviewContent);
}
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);
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;
}
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());
}
void MainWindow::on_addressEdit_returnPressed()
{
gotoUrl(ui->addressEdit->text());
}