From fe1530fb976ce2c326be2af5f7705e2936c8c131 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Wed, 23 Sep 2020 01:42:09 +0800 Subject: [PATCH] init commit --- .gitignore | 2 + README.md | 3 + main.cpp | 11 ++++ mainwindow.cpp | 79 ++++++++++++++++++++++ mainwindow.h | 31 +++++++++ mainwindow.ui | 156 ++++++++++++++++++++++++++++++++++++++++++++ pineapple-files.pro | 33 ++++++++++ statusbarwidget.cpp | 30 +++++++++ statusbarwidget.h | 19 ++++++ 9 files changed, 364 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 main.cpp create mode 100644 mainwindow.cpp create mode 100644 mainwindow.h create mode 100644 mainwindow.ui create mode 100644 pineapple-files.pro create mode 100644 statusbarwidget.cpp create mode 100644 statusbarwidget.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fb1a0d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# User files +*.user diff --git a/README.md b/README.md new file mode 100644 index 0000000..256d256 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +This is a toy project, just for testing QColumnView for file browsing. + +This project is NOT recommended to use. diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..aff48df --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..9dde8a1 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,79 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +#include "statusbarwidget.h" + +#include + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) + , m_fsmodel(new QFileSystemModel) +{ + ui->setupUi(this); + + ui->cdUpButton->setIcon(qApp->style()->standardIcon(QStyle::SP_FileDialogToParent)); + + m_fsmodel->setRootPath(""); + 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::viewGotoModelIndex); + connect(ui->treeTable, &QAbstractItemView::doubleClicked, this, &MainWindow::viewGotoModelIndex); + connect(ui->columnView, &QAbstractItemView::clicked, this, &MainWindow::viewGotoModelIndex); +} + +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); +} + +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()); +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..6605e28 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,31 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +class QFileSystemModel; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + + void viewGotoModelIndex(const QModelIndex &index); + +private slots: + void on_treeView_clicked(const QModelIndex &index); + + void on_cdUpButton_clicked(); + +private: + Ui::MainWindow *ui; + + QFileSystemModel * m_fsmodel; +}; +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..baf379d --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,156 @@ + + + MainWindow + + + + 0 + 0 + 1153 + 673 + + + + MainWindow + + + + + + + + + + + + + + + + + + + 0 + 0 + + + + Qt::Horizontal + + + + + 1 + 0 + + + + + 200 + 0 + + + + true + + + + + + 3 + 0 + + + + 0 + + + 0 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + QListView::Snap + + + QListView::IconMode + + + true + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + + + + + + + + + 0 + 0 + 1153 + 25 + + + + + + + + diff --git a/pineapple-files.pro b/pineapple-files.pro new file mode 100644 index 0000000..53cadf5 --- /dev/null +++ b/pineapple-files.pro @@ -0,0 +1,33 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# The following define makes your compiler emit warnings if you use +# any Qt feature that has been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + statusbarwidget.cpp + +HEADERS += \ + mainwindow.h \ + statusbarwidget.h + +FORMS += \ + mainwindow.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/statusbarwidget.cpp b/statusbarwidget.cpp new file mode 100644 index 0000000..5ef8a84 --- /dev/null +++ b/statusbarwidget.cpp @@ -0,0 +1,30 @@ +#include "statusbarwidget.h" + +#include +#include + +StatusbarWidget::StatusbarWidget(QWidget *parent) : QWidget(parent) +{ + QHBoxLayout * layout = new QHBoxLayout; + this->setLayout(layout); + layout->setMargin(0); + + QPushButton * m_columnsModeBtn = new QPushButton("Columns"); + connect(m_columnsModeBtn, &QPushButton::clicked, this, [this](){ + Q_EMIT requestMode(0); + }); + + QPushButton * m_listModeBtn = new QPushButton("List"); + connect(m_listModeBtn, &QPushButton::clicked, this, [this](){ + Q_EMIT requestMode(1); + }); + + QPushButton * m_detailsModeBtn = new QPushButton("Details"); + connect(m_detailsModeBtn, &QPushButton::clicked, this, [this](){ + Q_EMIT requestMode(2); + }); + + layout->addWidget(m_columnsModeBtn); + layout->addWidget(m_listModeBtn); + layout->addWidget(m_detailsModeBtn); +} diff --git a/statusbarwidget.h b/statusbarwidget.h new file mode 100644 index 0000000..2c6b96f --- /dev/null +++ b/statusbarwidget.h @@ -0,0 +1,19 @@ +#ifndef STATUSBARWIDGET_H +#define STATUSBARWIDGET_H + +#include +#include + +class StatusbarWidget : public QWidget +{ + Q_OBJECT +public: + explicit StatusbarWidget(QWidget *parent = nullptr); + +signals: + void requestMode(int mode); + +public slots: +}; + +#endif // STATUSBARWIDGET_H