Browse Source

pane class

master
hex 4 years ago
parent
commit
4c1ea69a62
4 changed files with 71 additions and 28 deletions
  1. 25
    0
      include/pane.h
  2. 0
    4
      include/window.h
  3. 42
    0
      src/pane.cpp
  4. 4
    24
      src/window.cpp

+ 25
- 0
include/pane.h View File

@@ -0,0 +1,25 @@
#pragma once

#include <QWidget>
#include <QModelIndex>

class QListView;
class QFileSystemModel;

namespace hex {

class Pane : public QWidget {
public:
explicit Pane(QWidget *parent = 0);

public slots:
bool setPath(const QString& path);
void changeIndex(const QModelIndex& index);
void moveUp();
private:
QListView* mView;
QFileSystemModel* mModel;
};

}

+ 0
- 4
include/window.h View File

@@ -13,11 +13,7 @@ public:
explicit Window(QWidget *parent = 0);

public slots:
void moveUp();
void changeIndex(const QModelIndex& index);
private:
QPushButton *mButton;
QListView *mListView;
QFileSystemModel *mModel;
};

+ 42
- 0
src/pane.cpp View File

@@ -0,0 +1,42 @@
#include "pane.h"

#include <QListView>
#include <QVBoxLayout>
#include <QFileSystemModel>

namespace hex {

Pane::Pane(QWidget* parent) :
QWidget(parent)
{
auto layout = new QVBoxLayout;
mView = new QListView();
mModel = new QFileSystemModel();
mView->setModel(mModel);
mModel->setRootPath("/");
setPath("/home/user");
layout->addWidget(mView);
layout->setContentsMargins(0,0,0,0);
this->setLayout(layout);

QObject::connect(mView, &QListView::doubleClicked,
this, &Pane::changeIndex);
}

bool Pane::setPath(const QString& path) {
changeIndex(mModel->index(path));
}

void Pane::changeIndex(const QModelIndex& index) {
if (mModel->isDir(index)) {
mView->setRootIndex(index);
} else {
//open file
}
}

void Pane::moveUp() {
changeIndex(mView->rootIndex().parent());
}

}

+ 4
- 24
src/window.cpp View File

@@ -1,4 +1,5 @@
#include "window.h"
#include "pane.h"

#include <QPushButton>
#include <QListView>
@@ -16,33 +17,12 @@ Window::Window(QWidget *parent) :
QVBoxLayout* navbar = new QVBoxLayout(this);
mButton = new QPushButton("^");
navbar->addWidget(mButton);
auto pane = new hex::Pane;
navbar->addWidget(pane);

QHBoxLayout* splitter = new QHBoxLayout(this);
mListView = new QListView(this);
splitter->addWidget(mListView);
navbar->addItem(splitter);
this->setLayout(navbar);

mModel = new QFileSystemModel();
mListView->setModel(mModel);

mModel->setRootPath("/");
mListView->setRootIndex(mModel->index("/home/user/"));

QObject::connect(mListView, &QListView::doubleClicked,
this, &Window::changeIndex);
QObject::connect(mButton, &QPushButton::clicked,
this, &Window::moveUp);
pane, &hex::Pane::moveUp);
}

void Window::moveUp() {
mListView->setRootIndex(mListView->rootIndex().parent());
}

void Window::changeIndex(const QModelIndex& index) {
if (mModel->isDir(index)) {
mListView->setRootIndex(index);
} else {
QDesktopServices::openUrl(QUrl::fromLocalFile(mModel->filePath(index)));
}
}

Loading…
Cancel
Save