diff options
author | Javier <dev.git@javispedro.com> | 2020-10-12 02:27:14 +0200 |
---|---|---|
committer | Javier <dev.git@javispedro.com> | 2020-10-12 02:27:14 +0200 |
commit | 55b5c88cf93d06629ba70eb7cddeb50a365b170a (patch) | |
tree | 49073a56b531898d27822812ba859bd26892f634 /notebookmodel.cc | |
parent | a22f3f54d9426c29220815f95dec574a11c43f69 (diff) | |
download | scribiu-55b5c88cf93d06629ba70eb7cddeb50a365b170a.tar.gz scribiu-55b5c88cf93d06629ba70eb7cddeb50a365b170a.zip |
minor UI refactor; allow reading ".archive" directories
Archives cannot be created from the UI yet.
Just copy a .pen directory to an .archive directory for now.
Diffstat (limited to 'notebookmodel.cc')
-rw-r--r-- | notebookmodel.cc | 95 |
1 files changed, 65 insertions, 30 deletions
diff --git a/notebookmodel.cc b/notebookmodel.cc index bfe183b..3898df7 100644 --- a/notebookmodel.cc +++ b/notebookmodel.cc @@ -29,6 +29,9 @@ #define NUM_COLUMNS 3 #define PEN_INDEX_ID 0xFFFFFFFFU +#define PEN_EXTENSION "pen" +#define ARCHIVE_EXTENSION "archive" + NotebookModel::NotebookModel(QObject *parent) : QAbstractItemModel(parent), _dataDir(userDataDirectory()), @@ -66,12 +69,12 @@ QString NotebookModel::userDataDirectory() QString NotebookModel::penDirectory(const QString &name) const { - return _dataDir.filePath(name + ".pen"); + return _dataDir.filePath(name); } QString NotebookModel::notebookDirectory(const QString &penName, const QString &nbName) const { - return _dataDir.filePath(penName + ".pen" + "/" + nbName + ".afd"); + return _dataDir.filePath(penName + "/" + nbName); } QString NotebookModel::notebookDirectory(const QModelIndex &index) const @@ -89,7 +92,7 @@ QString NotebookModel::notebookDirectory(const QModelIndex &index) const QString NotebookModel::paperReplayDirectory(const QString &name) const { - return _dataDir.filePath(name + ".pen" + "/"+ PAPER_REPLAY); + return _dataDir.filePath(name + "/" + PAPER_REPLAY); } QVariant NotebookModel::data(const QModelIndex &index, int role) const @@ -99,22 +102,28 @@ QVariant NotebookModel::data(const QModelIndex &index, int role) const if (id == PEN_INDEX_ID) { int penIndex = index.row(); if (penIndex < 0 || penIndex >= _pens.size()) return QVariant(); + const QString &penName = _pens[penIndex]; switch (role) { case Qt::DisplayRole: switch (index.column()) { case 0: - return _pens[penIndex]; + return penDisplayName(penName); } break; case Qt::DecorationRole: switch (index.column()) { + case 0: + return penIcon(penName); case 2: - if (isPenLocked(_pens[penIndex])) { + if (isPenLocked(penName)) { return QApplication::style()->standardIcon(QStyle::SP_BrowserReload); } break; } break; + case FileNameRole: + return penName; + break; } } else { const QString &penName = _pens[id]; @@ -124,7 +133,7 @@ QVariant NotebookModel::data(const QModelIndex &index, int role) const case Qt::DisplayRole: switch (index.column()) { case 0: - return notebookName; + return notebookDisplayName(penName, notebookName); case 1: if (notebookName != PAPER_REPLAY) { return estimatePagesOfNotebook(penName, notebookName); @@ -135,18 +144,12 @@ QVariant NotebookModel::data(const QModelIndex &index, int role) const case Qt::DecorationRole: switch (index.column()) { case 0: - return getNotebookIcon(penName, notebookName); + return notebookIcon(penName, notebookName); case 1: return QVariant(); case 2: - if (notebookName != PAPER_REPLAY) { - if (isNotebookLocked(penName, notebookName)) { - return QApplication::style()->standardIcon(QStyle::SP_BrowserReload); - } - } else { - if (isPaperReplayLocked(penName)) { - return QApplication::style()->standardIcon(QStyle::SP_BrowserReload); - } + if (isNotebookLocked(penName, notebookName)) { + return QApplication::style()->standardIcon(QStyle::SP_BrowserReload); } break; } @@ -160,6 +163,8 @@ QVariant NotebookModel::data(const QModelIndex &index, int role) const return Qt::AlignCenter; } break; + case FileNameRole: + return notebookName; } } return QVariant(); @@ -239,10 +244,7 @@ int NotebookModel::columnCount(const QModelIndex &parent) const void NotebookModel::refresh() { - QStringList pens = _dataDir.entryList(QStringList("*.pen"), QDir::Dirs, QDir::Name); - for (int i = 0; i < pens.size(); i++) { - pens[i].chop(4); // Remove .pen extension - } + QStringList pens = _dataDir.entryList(QStringList{"*." PEN_EXTENSION, "*." ARCHIVE_EXTENSION}, QDir::Dirs, QDir::Name); // Insert/remove new/deleted pens int i = 0, j = 0; @@ -295,10 +297,7 @@ void NotebookModel::refreshPen(const QString &name) _watcher.addPath(penDir.canonicalPath()); QStringList &curNotebooks = _notebooks[name]; - QStringList diskNotebooks = penDir.entryList(QStringList("*.afd"), QDir::Dirs, QDir::Name); - for (int i = 0; i < diskNotebooks.size(); i++) { - diskNotebooks[i].chop(4); - } + QStringList diskNotebooks = penDir.entryList(QStringList("*." AFD_NOTEBOOK_EXTENSION), QDir::Dirs, QDir::Name); if (penDir.exists(PAPER_REPLAY)) diskNotebooks.append(PAPER_REPLAY); QModelIndex penIndex = index(indexOfPen(name), 0, QModelIndex()); @@ -358,8 +357,44 @@ QDir NotebookModel::notebookDir(const QString &pen, const QString ¬ebook) con return QDir(notebookDirectory(pen, notebook)); } -QIcon NotebookModel::getNotebookIcon(const QString &pen, const QString ¬ebook) const +QString NotebookModel::penDisplayName(const QString &pen) const +{ + if (pen.endsWith("." PEN_EXTENSION, Qt::CaseInsensitive)) { + return pen.chopped(strlen("." PEN_EXTENSION)); + } else if (pen.endsWith("." ARCHIVE_EXTENSION, Qt::CaseInsensitive)) { + return pen.chopped(strlen("." ARCHIVE_EXTENSION)); + } else { + return pen; + } +} + +QString NotebookModel::notebookDisplayName(const QString &pen, const QString ¬ebook) const +{ + Q_UNUSED(pen); + if (notebook.endsWith("." AFD_NOTEBOOK_EXTENSION, Qt::CaseInsensitive)) { + return notebook.chopped(strlen("." AFD_NOTEBOOK_EXTENSION)); + } else if (notebook == PAPER_REPLAY) { + return tr("Voice recordings"); + } else { + return notebook; + } +} + +QIcon NotebookModel::penIcon(const QString &pen) const +{ + if (isPenArchive(pen)) { + return QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon); + } else { + return QIcon::fromTheme("scribiu"); + } +} + +QIcon NotebookModel::notebookIcon(const QString &pen, const QString ¬ebook) const { + if (notebook == PAPER_REPLAY) { + return QApplication::style()->standardIcon(QStyle::SP_MediaVolume); + } + QDir dir = notebookDir(pen, notebook); QIcon icon = _iconCache.value(dir.path()); @@ -380,6 +415,11 @@ QIcon NotebookModel::getNotebookIcon(const QString &pen, const QString ¬ebook return icon; } +bool NotebookModel::isPenArchive(const QString &pen) const +{ + return pen.endsWith("." ARCHIVE_EXTENSION); +} + bool NotebookModel::isPenLocked(const QString &pen) const { QDir dir = penDir(pen); @@ -402,12 +442,7 @@ bool NotebookModel::isNotebookLocked(const QString &pen, const QString ¬ebook bool NotebookModel::isPaperReplayLocked(const QString &pen) const { - QDir dir(paperReplayDirectory(pen)); - if (dir.exists(".sync.lck")) { - return true; - } else { - return false; - } + return isNotebookLocked(pen, PAPER_REPLAY); } int NotebookModel::estimatePagesOfNotebook(const QString &pen, const QString ¬ebook) const |