Skip to content

Commit

Permalink
add error page
Browse files Browse the repository at this point in the history
  • Loading branch information
rabauke committed Jan 26, 2024
1 parent b7d3c9d commit 57a5f23
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 40 deletions.
13 changes: 5 additions & 8 deletions qml/harbour-sailbabel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ ApplicationWindow {

AppModel {
id: appModel
}

Dictionary {
id: dictionary
onReadingFinished: {
pageStack.replace('pages/MainPage.qml')
pageStack.pushAttached('pages/History.qml')
onReadingDictionaryFinished: {
pageStack.replace(Qt.resolvedUrl('pages/MainPage.qml'))
pageStack.pushAttached(Qt.resolvedUrl('pages/History.qml'))
}
onReadingError: {
pageStack.replace('pages/Error.qml')
onReadingDictionaryFailed: {
pageStack.replace(Qt.resolvedUrl('pages/Error.qml'))
appModel.currentDictionary = ''
}
}
Expand Down
2 changes: 1 addition & 1 deletion qml/pages/ChooseDictionary.qml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Page {
if (fileIsDir) {
folderModel.folder = filePath
} else {
appModel.currentDictionary = filePath
appModel.currentDictionary = 'file://' + filePath
pageStack.replace('LoadDictionary.qml')
}
}
Expand Down
33 changes: 11 additions & 22 deletions qml/pages/Error.qml
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,19 @@ Page {
width: parent.width
spacing: Theme.paddingMedium
PageHeader {
title: 'SailBabel'
title: qsTr('Error')
}
Text {
x: Theme.horizontalPageMargin
width: column.width-2*x
color: Theme.primaryColor
wrapMode: TextEdit.Wrap
font.pixelSize: Theme.fontSizeMedium
horizontalAlignment: TextEdit.AlignJustify
text: qsTr('_error_description_')
textFormat: Text.StyledText
linkColor: Theme.highlightColor
onLinkActivated: { Qt.openUrlExternally(link) }
TextField {
text: qsTr('Unable to open dictionary.')
readOnly: true
}
Label {
text: '<br>© Heiko Bauke, 2016–2017<br><br>Fork me on github!<br><a href=\"https://github.com/rabauke/harbour-sailbabel\">https://github.com/rabauke/harbour-sailbabel</a>'
textFormat: Text.StyledText
width: column.width
color: Theme.primaryColor
linkColor: Theme.highlightColor
wrapMode: TextEdit.Wrap
font.pixelSize: Theme.fontSizeSmall
horizontalAlignment: TextEdit.AlignHCenter
onLinkActivated: { Qt.openUrlExternally(link) }
Button {
text: qsTr('Ok')
anchors.horizontalCenter: column.horizontalCenter
onClicked: {
pageStack.replace(Qt.resolvedUrl('MainPage.qml'))
pageStack.pushAttached(Qt.resolvedUrl('History.qml'))
}
}

}
Expand Down
4 changes: 2 additions & 2 deletions qml/pages/History.qml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Page {
onClicked: {
queryFieldText = searchHistoryListModel.get(model.index).query
resultsListModel.clear()
var trans = dictionary.translateAtoB(queryFieldText)
var trans = appModel.dictionary.translateAtoB(queryFieldText)
for (var i in trans)
resultsListModel.append({
'lang1': trans[i][0],
Expand All @@ -73,7 +73,7 @@ Page {
'lang1': '',
'lang2': ''
})
var trans = dictionary.translateBtoA(queryFieldText)
var trans = appModel.dictionary.translateBtoA(queryFieldText)
for (var i in trans)
resultsListModel.append({
'lang1': trans[i][0],
Expand Down
4 changes: 2 additions & 2 deletions qml/pages/LoadDictionary.qml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ Page {
anchors.horizontalCenter: label1.horizontalCenter
anchors.top: label1.bottom
anchors.topMargin: Theme.paddingMedium
text: qsTr('%n dictionary entries found.', '', dictionary.size)
text: qsTr('%n dictionary entries found.', '', appModel.dictionary.size)
color: Theme.highlightColor
}
onStatusChanged: {
if (status === PageStatus.Active)
dictionary.readAsync(appModel.currentDictionary)
appModel.dictionary.readAsync(appModel.currentDictionary)
}
}
4 changes: 2 additions & 2 deletions qml/pages/MainPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ Page {
'query': text
})
resultsListModel.clear()
var transAtoB = dictionary.translateAtoB(text)
var transBtoA = dictionary.translateBtoA(text)
var transAtoB = appModel.dictionary.translateAtoB(text)
var transBtoA = appModel.dictionary.translateBtoA(text)
var i
for (i in transAtoB)
resultsListModel.append({
Expand Down
11 changes: 11 additions & 0 deletions src/AppModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ AppModel::AppModel(QObject* parent) : QObject{parent} {
const QVariant auto_load_dictionary{settings.value("autoLoadDictionary")};
if (auto_load_dictionary.canConvert<bool>())
m_auto_load_dictionary = auto_load_dictionary.toBool();

connect(&m_dictionary, &Dictionary::readingFinished, this,
[this]() { emit readingDictionaryFinished(); });
connect(&m_dictionary, &Dictionary::readingError, this, [this]() {
emit readingDictionaryFailed();
});
}


Expand Down Expand Up @@ -70,3 +76,8 @@ void AppModel::set_auto_load_dictionary(bool auto_load_dictionary) {
emit autoLoadDictionaryChanged();
}
}


Dictionary* AppModel::get_dictionary() {
return &m_dictionary;
}
7 changes: 7 additions & 0 deletions src/AppModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QtQmlIntegration>
#endif
#include "Version.h"
#include "Dictionary.hpp"


class AppModel : public QObject {
Expand All @@ -25,10 +26,13 @@ class AppModel : public QObject {
NOTIFY currentDictionaryChanged)
Q_PROPERTY(bool autoLoadDictionary READ get_auto_load_dictionary WRITE
set_auto_load_dictionary NOTIFY autoLoadDictionaryChanged)
Q_PROPERTY(Dictionary* dictionary READ get_dictionary CONSTANT)

signals:
void currentDictionaryChanged();
void autoLoadDictionaryChanged();
void readingDictionaryFinished();
void readingDictionaryFailed();

private:
QString get_current_dictionary() const;
Expand All @@ -37,7 +41,10 @@ class AppModel : public QObject {
bool get_auto_load_dictionary() const;
void set_auto_load_dictionary(bool auto_load_dictionary);

Dictionary* get_dictionary();

QString m_version{QString::fromStdString(project_version)};
QString m_current_dictionary;
bool m_auto_load_dictionary{true};
Dictionary m_dictionary;
};
5 changes: 3 additions & 2 deletions src/Dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
#include <QSet>
#include <QRegularExpression>
#include <QMutexLocker>
#include <QDebug>


Dictionary::Dictionary(QObject *parent) : QObject{parent} {
}


void Dictionary::readAsync(const QString &filename) {
void Dictionary::readAsync(const QUrl &filename) {
QThread *thread{new QThread};
DictionaryLoader *worker{new DictionaryLoader(*this, filename)};
DictionaryLoader *worker{new DictionaryLoader(*this, filename.toLocalFile())};
worker->moveToThread(thread);
connect(worker, &DictionaryLoader::error, this, [this]() { emit readingError(); });
connect(thread, &QThread::started, worker, &DictionaryLoader::process);
Expand Down
3 changes: 2 additions & 1 deletion src/Dictionary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QVector>
#include <QMultiHash>
#include <QVariantList>
#include <QUrl>


class Dictionary;
Expand All @@ -24,7 +25,7 @@ class Dictionary : public QObject {
Q_PROPERTY(int size READ size NOTIFY sizeChanged)
Q_PROPERTY(bool dictionaryLoaded READ dictEmpty NOTIFY dictChanged)

Q_INVOKABLE void readAsync(const QString &filename);
Q_INVOKABLE void readAsync(const QUrl &filename);
Q_INVOKABLE QVariantList translateAtoB(const QString &query) const;
Q_INVOKABLE QVariantList translateBtoA(const QString &query) const;

Expand Down

0 comments on commit 57a5f23

Please sign in to comment.