Skip to content

Commit

Permalink
General UI improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjohnman committed Sep 27, 2014
1 parent e4b673a commit d7ffc64
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 17 deletions.
14 changes: 10 additions & 4 deletions dialogloading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <QtDebug>


DialogLoading::DialogLoading(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogLoading)
Expand All @@ -21,13 +20,19 @@ void DialogLoading::start()
ui->progressBar->setMaximum(0);
ui->progressBar->setValue(0);

connect(&workerThread, SIGNAL(finished()), this, SLOT(workerDone()));
connect(&workerThread, SIGNAL(loadSucceeded()), this, SLOT(workerDone()));
connect(&workerThread, SIGNAL(readLine(int, QString)), this, SLOT(onLineRead(int,QString)));
connect(&workerThread, SIGNAL(gotFileSize(int)), this, SLOT(onGotFileSize(int)));

connect(&workerThread, SIGNAL(loadFailed()), this, SLOT(onLoadFailed()));
workerThread.start();
}

void DialogLoading::onLoadFailed()
{
emit failed();
close();
}

void DialogLoading::onGotFileSize(int size)
{
ui->progressBar->setMaximum(size);
Expand All @@ -37,10 +42,11 @@ void DialogLoading::onLineRead(int filePos, QString line)
{
m_Lines.push_back(line);
ui->progressBar->setValue(filePos);
ui->label->setText(QString("Reading dictionary entries... (%1)").arg(m_Lines.size()));
ui->label->setText(QString("Reading dictionary entries... (%1 entries read)").arg(m_Lines.size()));
}

void DialogLoading::workerDone()
{
emit succeeded();
close();
}
5 changes: 5 additions & 0 deletions dialogloading.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ class DialogLoading : public QDialog

QStringList m_Lines;

signals:
void succeeded();
void failed();

public slots:
void workerDone();
void onLineRead(int filePos, QString line);
void onGotFileSize(int size);
void onLoadFailed();

private:
Ui::DialogLoading *ui;
Expand Down
9 changes: 9 additions & 0 deletions dialogloadingworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ void DialogLoadingWorker::run()
{
qDebug() << "Opening cedict_ts.u8... ";
QFile dictFile("cedict_ts.u8");

if(!dictFile.exists())
{
emit loadFailed();
return;
}

if(dictFile.open(QIODevice::ReadOnly))
{
emit gotFileSize(dictFile.size());
Expand All @@ -22,5 +29,7 @@ void DialogLoadingWorker::run()
}

dictFile.close();

emit loadSucceeded();
}
}
2 changes: 2 additions & 0 deletions dialogloadingworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class DialogLoadingWorker : public QThread
signals:
void gotFileSize(int size);
void readLine(int filePos, QString line);
void loadFailed();
void loadSucceeded();
};

#endif // DIALOGLOADINGWORKER_H
20 changes: 19 additions & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ MainWindow::MainWindow(QWidget *parent) :

connect(ui->pushButton, SIGNAL(clicked()), SLOT(doSearch()));
connect(ui->lineEdit, SIGNAL(returnPressed()), SLOT(doSearch()));
connect(&loadingDialog, SIGNAL(finished(int)), this, SLOT(loadingComplete()));
connect(&loadingDialog, SIGNAL(succeeded()), this, SLOT(loadingComplete()));
connect(&loadingDialog, SIGNAL(failed()), this, SLOT(loadingFailed()));

connect(ui->listView, SIGNAL(clicked(QModelIndex)), this, SLOT(onSelectionChanged(QModelIndex)));

ui->searchProgressBar->hide();

msgBox = 0;
}

MainWindow::~MainWindow()
Expand All @@ -27,6 +31,8 @@ MainWindow::~MainWindow()
void MainWindow::doSearch()
{
ui->lineEdit->setEnabled(false);
ui->pushButton->hide();
ui->searchProgressBar->show();
_m_DataModel->search(ui->lineEdit->text());
ui->listView->reset();
}
Expand All @@ -48,6 +54,16 @@ void MainWindow::onSelectionChanged(QModelIndex index)
}
}

void MainWindow::loadingFailed()
{
if(msgBox) delete msgBox;
msgBox = new QMessageBox();
msgBox->setText("The file \"cedict_ts.u8\" could not be loaded.");
msgBox->setIcon(QMessageBox::Critical);
msgBox->setDetailedText("Please make sure the dictionary file is in the same directory as this application and that you have permissions to read it.");
msgBox->show();
}

void MainWindow::loadingComplete()
{
_m_DataModel = new DataModel(loadingDialog.m_Lines, ui->listView);
Expand All @@ -59,6 +75,8 @@ void MainWindow::loadingComplete()
void MainWindow::onSearchFinished()
{
ui->lineEdit->setEnabled(true);
ui->searchProgressBar->hide();
ui->pushButton->show();
}

void MainWindow::loadDictionary()
Expand Down
4 changes: 4 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>

#include "dialogloading.h"

#include "datamodel.h"
Expand All @@ -24,12 +26,14 @@ public slots:
void doSearch();
void onSearchFinished();
void loadingComplete();
void loadingFailed();
void onSelectionChanged(QModelIndex index);

private:
Ui::MainWindow *ui;
DialogLoading loadingDialog;
DataModel * _m_DataModel;
QMessageBox * msgBox;
};

#endif // MAINWINDOW_H
46 changes: 34 additions & 12 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,21 @@
</property>
</widget>
</item>
<item row="0" column="1">
<item row="0" column="2">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Search</string>
</property>
</widget>
</item>
<item row="0" column="2" rowspan="5">
<widget class="QListView" name="listView">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<item row="1" column="0" colspan="3">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<item row="2" column="0" colspan="3">
<widget class="QLabel" name="label_Simplified">
<property name="font">
<font>
Expand All @@ -76,7 +69,7 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<item row="3" column="0" colspan="3">
<widget class="QLabel" name="label_Traditional">
<property name="font">
<font>
Expand All @@ -91,7 +84,14 @@
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<item row="0" column="3" rowspan="5">
<widget class="QListView" name="listView">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
</widget>
</item>
<item row="4" column="0" colspan="3">
<widget class="QTextBrowser" name="textBrowser">
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
Expand All @@ -104,6 +104,28 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QProgressBar" name="searchProgressBar">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximum">
<number>0</number>
</property>
<property name="value">
<number>0</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
Expand Down

1 comment on commit d7ffc64

@tjohnman
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alert on dictionary failed loading. Feedback while searching.

Please sign in to comment.