Skip to content

Commit

Permalink
load file dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-x committed Mar 4, 2012
1 parent 4067ed2 commit fcc1685
Show file tree
Hide file tree
Showing 7 changed files with 1,363 additions and 2 deletions.
167 changes: 167 additions & 0 deletions filebrowser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* scribble: Scribbling Application for Onyx Boox M92
*
* Copyright (C) 2012 peter-x
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "filebrowser.h"

#include "onyx/screen/screen_proxy.h"
#include "onyx/ui/buttons.h"

class ModelBuilder {
public:
ModelBuilder(QStandardItemModel &model, int numCols = 1 /* TODO bug for greater than one */)
: col(0), row(0), numCols(numCols), model(model) {
model.clear();
model.setColumnCount(numCols);
}

void addItem(const QString &text) {
addItem(QIcon(), text);
}

void addItem(const QIcon &icon, const QString &text, const QString &data = QString()) {
QFont itemFont;
itemFont.setPointSize(20);
QStandardItem *item = new QStandardItem(icon, text);
item->setData(data);
item->setFont(itemFont);
model.setItem(row, col, item);
col += 1;
if (col >= numCols) {
col = 0;
row ++;
}
}

private:
int col;
int row;
int numCols;
QStandardItemModel &model;
};

FileBrowser::FileBrowser(QWidget *parent)
: QDialog(parent), layout(this),
treeView(0, &model),
statusBar(0, ui::PROGRESS | ui::MESSAGE)
{
setModal(true);

setAutoFillBackground(true);
setBackgroundRole(QPalette::Dark);

layout.setSpacing(0);
layout.setContentsMargins(0, 0, 0, 0);

treeView.showHeader(false);
layout.addWidget(&treeView, 1);

layout.addWidget(&statusBar);

connect(&treeView, SIGNAL(activated(const QModelIndex &)),
SLOT(onItemActivated(const QModelIndex &)));
connect(&treeView, SIGNAL(positionChanged(int, int)),
&statusBar, SLOT(setProgress(int, int)));
connect(&statusBar, SIGNAL(progressClicked(const int, const int)),
SLOT(onStatusBarClicked(const int, const int)));
}

FileBrowser::~FileBrowser()
{

}

QString FileBrowser::showLoadFile()
{
updateTreeView();

showMaximized();
onyx::screen::instance().flush(this, onyx::screen::ScreenProxy::GC);

exec();

return currentRealPath;
}

void FileBrowser::updateRealPath()
{
if (currentPath.isEmpty()) {
currentRealPath = QString();
} else {
currentRealPath = currentPath.join("/");
}
}

void FileBrowser::updateTreeView()
{
treeView.clear();
updateModel();
treeView.setModel(&model);
treeView.update();
statusBar.setProgress(treeView.currentPage(), treeView.pages());
}

void FileBrowser::updateModel()
{
ModelBuilder builder(model);

if (currentPath.isEmpty()) {
/* TODO only if they exist */
builder.addItem(QIcon(":/images/sketch.png"), "Internal Scribbles", QDir::homePath() + QString("/notes"));
builder.addItem(QIcon(), "Flash", LIBRARY_ROOT);
builder.addItem(QIcon(), "SD Card", SDMMC_ROOT);
builder.addItem(QIcon(), "Root", "/");
return;
}
builder.addItem(QIcon(), "Up", "..");

QDir dir(currentRealPath);
dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
dir.setSorting(QDir::Name | QDir::DirsFirst | QDir::IgnoreCase);

QFileInfoList list = dir.entryInfoList();
foreach (QFileInfo fileInfo, list) {
builder.addItem(QIcon(), fileInfo.fileName(), fileInfo.fileName());
}
}

void FileBrowser::onItemActivated(const QModelIndex &idx)
{
QStandardItem *item = model.itemFromIndex(idx);
QString data = item->data().toString();
if (data == "..") {
currentPath = currentPath.mid(0, currentPath.length() - 1);
} else {
currentPath += data;
}
updateRealPath();
if (currentRealPath.isEmpty() || QDir(currentRealPath).exists()) {
updateTreeView();
} else if (QFile(currentRealPath).exists()) {
accept();
} else {
/* TODO error */
reject();
}
}

void FileBrowser::onStatusBarClicked(const int percentage, const int page)
{
treeView.jumpToPage(page);
}
67 changes: 67 additions & 0 deletions filebrowser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* scribble: Scribbling Application for Onyx Boox M92
*
* Copyright (C) 2012 peter-x
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#ifndef FILEBROWSER_H
#define FILEBROWSER_H

#include <QObject>

#include "tree_view.h"

#include "onyx/ui/tree_view.h"
#include "onyx/ui/status_bar.h"


class FileBrowser : public QDialog
{
Q_OBJECT
public:
explicit FileBrowser(QWidget *parent = 0);
~FileBrowser();

QString showLoadFile();
QString selectedFile();

private slots:
void onItemActivated(const QModelIndex &);
void onStatusBarClicked(const int, const int);

public slots:

private:
void updateRealPath();
void updateTreeView();
void updateModel();

void pathUp();
void setCurrentPath();
//QString getIcon(const QFileInfo &fileInfo);
//QString getDisplayName(const QFileInfo &fileInfo);

private:
QString currentRealPath;
QStringList currentPath;
QVBoxLayout layout;
QStandardItemModel model;
obx::ObxTreeView treeView;
ui::StatusBar statusBar;
};

#endif // FILEBROWSER_H
34 changes: 34 additions & 0 deletions mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@

#include "mainwidget.h"

#include "filebrowser.h"

#include "onyx/screen/screen_proxy.h"
#include "onyx/screen/screen_update_watcher.h"

#include "onyx/ui/toolbar.h"
#include "onyx/ui/status_bar.h"
#include "onyx/ui/onyx_dialog.h"
#include "onyx/ui/thumbnail_view.h"

MainWidget::MainWidget(QWidget *parent) :
QWidget(parent, Qt::FramelessWindowHint), currentFile("")
Expand Down Expand Up @@ -68,6 +72,17 @@ MainWidget::MainWidget(QWidget *parent) :
toolbar->addAction(thick);
*/

/* TODO icon at least looks like "open" */
QAction *open = new QAction(QIcon(":/images/config.png"),
"open document", this);
connect(open, SIGNAL(triggered()), SLOT(open()));
toolbar->addAction(open);

QAction *save = new QAction(QIcon(":/images/save_document.png"),
"save as", this);
connect(save, SIGNAL(triggered()), SLOT(saveAs()));
toolbar->addAction(save);

QAction *right = new QAction(QIcon(":/images/right_arrow.png"),
"next page", this);
connect(right, SIGNAL(triggered()), document, SLOT(nextPage()));
Expand Down Expand Up @@ -209,6 +224,25 @@ void MainWidget::mouseReleaseEvent(QMouseEvent *ev)
touchEventDataReceived(data);
}

void MainWidget::open()
{
FileBrowser fileBrowser(this);
QString path = fileBrowser.showLoadFile();
if (path.isEmpty())
return;
loadFile(QFile(path));
}

void MainWidget::saveAs()
{
QString file = QFileDialog::getSaveFileName(this, "Save Scribble File",
currentFile.fileName(),
"Xournal Files (*.xoj);;All Files (*)");
if (!file.isEmpty()) {
saveFile(QFile(file));
}
}

void MainWidget::updateProgressBar(int currentPage, int maxPages, int currentLayer, int maxLayers)
{
statusBar->setProgress(currentPage + 1, maxPages);
Expand Down
3 changes: 3 additions & 0 deletions mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ private slots:
void mouseMoveEvent(QMouseEvent *ev);
void mouseReleaseEvent(QMouseEvent *ev);

void saveAs();
void open();

void updateProgressBar(int currentPage, int maxPages, int currentLayer, int maxLayers);
void setPage(int percentage, int page);

Expand Down
8 changes: 6 additions & 2 deletions scribble.pro
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ QT += core network sql gui
SOURCES += scribble.cpp \
mainwidget.cpp \
scribblearea.cpp \
scribble_document.cpp
scribble_document.cpp \
filebrowser.cpp \
tree_view.cpp

LIBS += -L /usr/local/lib -lz -lonyxapp -lonyx_base -lonyx_ui -lonyx_screen -lonyx_sys -lonyx_wpa -lonyx_wireless -lonyx_data -lonyx_touch -lonyx_cms

Expand All @@ -12,6 +14,8 @@ INCLUDEPATH += /opt/onyx/arm/include
HEADERS += \
mainwidget.h \
scribblearea.h \
scribble_document.h
scribble_document.h \
filebrowser.h \
tree_view.h

RESOURCES +=
Loading

0 comments on commit fcc1685

Please sign in to comment.