diff options
author | Dennis Oberst <[email protected]> | 2023-06-27 15:54:20 +0200 |
---|---|---|
committer | Dennis Oberst <[email protected]> | 2023-08-21 14:10:04 +0200 |
commit | 959f88e4b5371edff0b344e5f768c70aa5202402 (patch) | |
tree | 4c634d55d552af545eed705a98b1916f62ea0ed9 /examples/quickcontrols/filesystemexplorer/filesystemmodel.cpp | |
parent | 4f9687927da504c344a6e1805fe32f0b2d650d8e (diff) |
Filesystem Explorer Example: Introduce version 2
This updated version addresses several bugs and misbehaviors that were
identified in the previous version. I have rewritten and improved
various aspects of the application to provide a more stable and reliable
user experience.
Here are some of the key changes and enhancements in version 2:
- Fix qmllint warnings.
- Reduce the number of redundant items
- Apply the custom window decorations inside MyMenuBar
to the contentItem instead of the background to scale
properly.
- Fix additional scaling and UI misbehaviors
- Add an application icon
- Add an editor with line numbers
- Add command line options to specify an initial directory
- Since rootIndex is exposed inside TreeView since 6.6
this is an excellent opportunity to make use of it
- Crosslink the python version of this example in the docs
Pick-to: 6.6
Change-Id: Ib816a95f843b3f4b84b11db893facda0ffc6f482
Reviewed-by: Mitch Curtis <[email protected]>
Reviewed-by: Qt CI Bot <[email protected]>
Diffstat (limited to 'examples/quickcontrols/filesystemexplorer/filesystemmodel.cpp')
-rw-r--r-- | examples/quickcontrols/filesystemexplorer/filesystemmodel.cpp | 57 |
1 files changed, 49 insertions, 8 deletions
diff --git a/examples/quickcontrols/filesystemexplorer/filesystemmodel.cpp b/examples/quickcontrols/filesystemexplorer/filesystemmodel.cpp index e383a0ea29..b580bced05 100644 --- a/examples/quickcontrols/filesystemexplorer/filesystemmodel.cpp +++ b/examples/quickcontrols/filesystemexplorer/filesystemmodel.cpp @@ -3,18 +3,15 @@ #include "filesystemmodel.h" -#include <QMimeDatabase> #include <QStandardPaths> +#include <QMimeDatabase> +#include <QTextDocument> +#include <QTextObject> FileSystemModel::FileSystemModel(QObject *parent) : QFileSystemModel(parent) { - setRootPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); -} - -int FileSystemModel::columnCount(const QModelIndex &parent) const -{ - Q_UNUSED(parent); - return 1; + setFilter(QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot); + setInitialDirectory(); } QString FileSystemModel::readFile(const QString &filePath) @@ -46,3 +43,47 @@ QString FileSystemModel::readFile(const QString &filePath) } return tr("Filetype not supported!"); } + +// This function gets called from Editor.qml +int FileSystemModel::currentLineNumber(QQuickTextDocument *textDocument, int cursorPosition) +{ + if (QTextDocument *td = textDocument->textDocument()) { + QTextBlock tb = td->findBlock(cursorPosition); + return tb.blockNumber(); + } + return -1; +} + +int FileSystemModel::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return 1; +} + +QModelIndex FileSystemModel::rootIndex() const +{ + return m_rootIndex; +} + +void FileSystemModel::setRootIndex(const QModelIndex index) +{ + if (index == m_rootIndex) + return; + m_rootIndex = index; + emit rootIndexChanged(); +} + +void FileSystemModel::setInitialDirectory(const QString &path) +{ + QDir dir(path); + if (dir.makeAbsolute()) + setRootPath(dir.path()); + else + setRootPath(getDefaultRootDir()); + setRootIndex(QFileSystemModel::index(dir.path(), 0)); +} + +QString FileSystemModel::getDefaultRootDir() +{ + return QStandardPaths::writableLocation(QStandardPaths::HomeLocation); +} |