Skip to content

Commit 1274614

Browse files
committed
use thread to offload work, so UI will not hang
1 parent b94688d commit 1274614

File tree

8 files changed

+192
-99
lines changed

8 files changed

+192
-99
lines changed

Makefile

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#############################################################################
22
# Makefile for building: qcryptdisk
3-
# Generated by qmake (2.01a) (Qt 4.8.2) on: ?? ???. 10 19:09:31 2012
3+
# Generated by qmake (2.01a) (Qt 4.8.2) on: Tue Feb 5 22:17:04 2013
44
# Project: qcryptdisk.pro
55
# Template: app
6-
# Command: /usr/bin/qmake-qt4 -spec /usr/share/qt4/mkspecs/linux-g++ -unix CONFIG+=debug -o Makefile qcryptdisk.pro
6+
# Command: /usr/bin/qmake-qt4 -spec /usr/share/qt4/mkspecs/linux-g++ CONFIG+=debug CFLAGS=-std=c++0x -o Makefile qcryptdisk.pro
77
#############################################################################
88

99
####### Compiler, tools and options
@@ -12,8 +12,8 @@ CC = gcc
1212
CXX = g++
1313
DEFINES = -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
1414
CFLAGS = -pipe -g -Wall -W -D_REENTRANT $(DEFINES)
15-
CXXFLAGS = -pipe -g -Wall -W -D_REENTRANT $(DEFINES)
16-
INCPATH = -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I.
15+
CXXFLAGS = -pipe -std=c++0x -g -Wall -W -D_REENTRANT $(DEFINES)
16+
INCPATH = -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I.
1717
LINK = g++
1818
LFLAGS =
1919
LIBS = $(SUBLIBS) -L/usr/lib/i386-linux-gnu -lQtGui -lQtCore -lpthread
@@ -43,13 +43,15 @@ OBJECTS_DIR = ./
4343

4444
####### Files
4545

46-
SOURCES = main.cpp \
47-
dialog.cpp \
48-
util.cpp moc_dialog.cpp
49-
OBJECTS = main.o \
50-
dialog.o \
46+
SOURCES = dialog.cpp \
47+
main.cpp \
48+
util.cpp moc_dialog.cpp \
49+
moc_util.cpp
50+
OBJECTS = dialog.o \
51+
main.o \
5152
util.o \
52-
moc_dialog.o
53+
moc_dialog.o \
54+
moc_util.o
5355
DIST = /usr/share/qt4/mkspecs/common/unix.conf \
5456
/usr/share/qt4/mkspecs/common/linux.conf \
5557
/usr/share/qt4/mkspecs/common/gcc-base.conf \
@@ -131,7 +133,7 @@ Makefile: qcryptdisk.pro /usr/share/qt4/mkspecs/linux-g++/qmake.conf /usr/share
131133
/usr/share/qt4/mkspecs/features/include_source_dir.prf \
132134
/usr/lib/i386-linux-gnu/libQtGui.prl \
133135
/usr/lib/i386-linux-gnu/libQtCore.prl
134-
$(QMAKE) -spec /usr/share/qt4/mkspecs/linux-g++ -unix CONFIG+=debug -o Makefile qcryptdisk.pro
136+
$(QMAKE) -spec /usr/share/qt4/mkspecs/linux-g++ CONFIG+=debug CFLAGS=-std=c++0x -o Makefile qcryptdisk.pro
135137
/usr/share/qt4/mkspecs/common/unix.conf:
136138
/usr/share/qt4/mkspecs/common/linux.conf:
137139
/usr/share/qt4/mkspecs/common/gcc-base.conf:
@@ -158,11 +160,11 @@ Makefile: qcryptdisk.pro /usr/share/qt4/mkspecs/linux-g++/qmake.conf /usr/share
158160
/usr/lib/i386-linux-gnu/libQtGui.prl:
159161
/usr/lib/i386-linux-gnu/libQtCore.prl:
160162
qmake: FORCE
161-
@$(QMAKE) -spec /usr/share/qt4/mkspecs/linux-g++ -unix CONFIG+=debug -o Makefile qcryptdisk.pro
163+
@$(QMAKE) -spec /usr/share/qt4/mkspecs/linux-g++ CONFIG+=debug CFLAGS=-std=c++0x -o Makefile qcryptdisk.pro
162164

163165
dist:
164166
@$(CHK_DIR_EXISTS) .tmp/qcryptdisk1.0.0 || $(MKDIR) .tmp/qcryptdisk1.0.0
165-
$(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/qcryptdisk1.0.0/ && $(COPY_FILE) --parents dialog.h util.h .tmp/qcryptdisk1.0.0/ && $(COPY_FILE) --parents main.cpp dialog.cpp util.cpp .tmp/qcryptdisk1.0.0/ && $(COPY_FILE) --parents dialog.ui .tmp/qcryptdisk1.0.0/ && (cd `dirname .tmp/qcryptdisk1.0.0` && $(TAR) qcryptdisk1.0.0.tar qcryptdisk1.0.0 && $(COMPRESS) qcryptdisk1.0.0.tar) && $(MOVE) `dirname .tmp/qcryptdisk1.0.0`/qcryptdisk1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/qcryptdisk1.0.0
167+
$(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/qcryptdisk1.0.0/ && $(COPY_FILE) --parents dialog.h util.h .tmp/qcryptdisk1.0.0/ && $(COPY_FILE) --parents dialog.cpp main.cpp util.cpp .tmp/qcryptdisk1.0.0/ && $(COPY_FILE) --parents dialog.ui .tmp/qcryptdisk1.0.0/ && (cd `dirname .tmp/qcryptdisk1.0.0` && $(TAR) qcryptdisk1.0.0.tar qcryptdisk1.0.0 && $(COMPRESS) qcryptdisk1.0.0.tar) && $(MOVE) `dirname .tmp/qcryptdisk1.0.0`/qcryptdisk1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/qcryptdisk1.0.0
166168

167169

168170
clean:compiler_clean
@@ -183,13 +185,16 @@ mocclean: compiler_moc_header_clean compiler_moc_source_clean
183185

184186
mocables: compiler_moc_header_make_all compiler_moc_source_make_all
185187

186-
compiler_moc_header_make_all: moc_dialog.cpp
188+
compiler_moc_header_make_all: moc_dialog.cpp moc_util.cpp
187189
compiler_moc_header_clean:
188-
-$(DEL_FILE) moc_dialog.cpp
190+
-$(DEL_FILE) moc_dialog.cpp moc_util.cpp
189191
moc_dialog.cpp: util.h \
190192
dialog.h
191193
/usr/bin/moc-qt4 $(DEFINES) $(INCPATH) dialog.h -o moc_dialog.cpp
192194

195+
moc_util.cpp: util.h
196+
/usr/bin/moc-qt4 $(DEFINES) $(INCPATH) util.h -o moc_util.cpp
197+
193198
compiler_rcc_make_all:
194199
compiler_rcc_clean:
195200
compiler_image_collection_make_all: qmake_image_collection.cpp
@@ -213,21 +218,24 @@ compiler_clean: compiler_moc_header_clean compiler_uic_clean
213218

214219
####### Compile
215220

216-
main.o: main.cpp dialog.h \
217-
util.h
218-
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp
219-
220221
dialog.o: dialog.cpp dialog.h \
221222
util.h \
222223
ui_dialog.h
223224
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o dialog.o dialog.cpp
224225

226+
main.o: main.cpp dialog.h \
227+
util.h
228+
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp
229+
225230
util.o: util.cpp util.h
226231
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o util.o util.cpp
227232

228233
moc_dialog.o: moc_dialog.cpp
229234
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_dialog.o moc_dialog.cpp
230235

236+
moc_util.o: moc_util.cpp
237+
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_util.o moc_util.cpp
238+
231239
####### Install
232240

233241
install: FORCE

dialog.cpp

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
#include "ui_dialog.h"
33

44
Dialog::Dialog(QWidget *parent)
5-
: QDialog(parent), ui(new Ui::DialogClass)
5+
: QDialog(parent), ui(new Ui::DialogClass), mount(this)
66
{
7-
ui->setupUi(this);
7+
ui->setupUi(this);
8+
connect(&watcher, SIGNAL(finished()), this, SLOT(operationDone()));
9+
connect(&mount, SIGNAL(signal(std::function<void(void)>)),
10+
this, SLOT(mount_cb(std::function<void(void)>)),
11+
Qt::BlockingQueuedConnection);
12+
refresh();
813
}
914

1015
Dialog::~Dialog()
@@ -15,18 +20,16 @@ Dialog::~Dialog()
1520
void
1621
Dialog::refresh()
1722
{
18-
ctab.refresh();
19-
fstab.refresh();
20-
mounts.refresh();
23+
mount.refresh();
2124

22-
ui->tableWidget->setRowCount(ctab.cnt.size());
25+
ui->tableWidget->setRowCount(mount.ctab.cnt.size());
2326
QTableWidgetItem* item = 0;
2427
int r = 0;
25-
for(CryptTab::ContainerT::iterator i = ctab.cnt.begin();
26-
i != ctab.cnt.end();
28+
for(CryptTab::ContainerT::iterator i = mount.ctab.cnt.begin();
29+
i != mount.ctab.cnt.end();
2730
++i)
2831
{
29-
QString mp = fstab.mountpoint(i->first);
32+
QString mp = mount.fstab.mountpoint(i->first);
3033
if ( mp.length() ) {
3134
QString tooltip(i->second);
3235

@@ -38,7 +41,7 @@ Dialog::refresh()
3841
item = new QTableWidgetItem(mp);
3942
ui->tableWidget->setItem(r,1,item);
4043

41-
QString state = State2String(mounts.state(i->second, i->first));
44+
QString state = State2String(mount.mounts.state(i->second, i->first));
4245
item = new QTableWidgetItem(state);
4346
ui->tableWidget->setItem(r,2,item);
4447
++r;
@@ -55,21 +58,35 @@ void Dialog::on_button_refresh_clicked()
5558
refresh();
5659
}
5760

61+
void Dialog::run_work(std::function<void(void)> f)
62+
{
63+
ui->button_mount->setEnabled(false);
64+
ui->button_umount->setEnabled(false);
65+
66+
QFuture<void> future = QtConcurrent::run(f);
67+
watcher.setFuture(future);
68+
}
5869

5970
void Dialog::on_button_mount_clicked()
6071
{
6172
QString name = get_current_name();
6273
std::cerr << "run mount for " << name.toStdString() << std::endl;
63-
run_mount(this, name);
64-
refresh();
74+
run_work(std::bind(&Mount::run_mount, &mount, name));
6575
}
6676

6777
void Dialog::on_button_umount_clicked()
6878
{
6979
QString name = get_current_name();
7080
std::cerr << "run umount for " << name.toStdString() << std::endl;
71-
run_unmount(name);
81+
run_work(std::bind(&Mount::run_unmount, &mount, name));
82+
}
83+
84+
void Dialog::operationDone(void)
85+
{
7286
refresh();
87+
ui->button_mount->setEnabled(true);
88+
ui->button_umount->setEnabled(true);
89+
7390
}
7491

7592
QString Dialog::get_current_name()
@@ -87,16 +104,21 @@ QString Dialog::get_current_name()
87104

88105
void Dialog::on_tableWidget_doubleClicked(QModelIndex index)
89106
{
107+
Q_UNUSED(index);
90108
QString name = get_current_name();
91-
QString location = ctab.location(name);
109+
QString location = mount.ctab.location(name);
92110

93-
State state = mounts.state(location, name);
111+
State state = mount.mounts.state(location, name);
94112
if (state == mounted) {
95-
run_unmount(name);
96-
refresh();
113+
on_button_umount_clicked();
97114
} else if (state == connected) {
98-
run_mount(this, name);
99-
refresh();
115+
on_button_mount_clicked();
100116
}
101117
}
102118

119+
void Dialog::mount_cb(std::function<void(void)> f)
120+
{
121+
f();
122+
}
123+
124+

dialog.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#define DIALOG_H
33

44
#include <QtGui/QDialog>
5+
#include <QFutureWatcher>
6+
#include <QtConcurrentRun>
7+
#include <QtCore/qnamespace.h>
58

69
namespace Ui
710
{
@@ -10,23 +13,27 @@ namespace Ui
1013

1114
#include <util.h>
1215

13-
class Dialog : public QDialog,
14-
public Mount
16+
class Dialog : public QDialog
1517
{
1618
Q_OBJECT
17-
1819
public:
1920
Dialog(QWidget *parent = 0);
2021
~Dialog();
2122

22-
void refresh();
2323

2424
private:
2525
Ui::DialogClass *ui;
26+
Mount mount;
2627

2728
QString get_current_name();
29+
QFutureWatcher<void> watcher;
30+
void run_work(std::function<void(void)> f);
31+
void refresh();
2832

2933
private slots:
34+
void operationDone(void);
35+
void mount_cb(std::function<void(void)>);
36+
3037
void on_tableWidget_doubleClicked(QModelIndex index);
3138
void on_button_mount_clicked();
3239
void on_button_umount_clicked();

main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ int main(int argc, char *argv[])
66
{
77
QApplication a(argc, argv);
88
Dialog w;
9-
w.refresh();
109
w.show();
1110
return a.exec();
1211
}

moc_dialog.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/****************************************************************************
22
** Meta object code from reading C++ file 'dialog.h'
33
**
4-
** Created: Fri Jun 1 01:16:40 2012
5-
** by: The Qt Meta Object Compiler version 63 (Qt 4.8.1)
4+
** Created: Tue Feb 5 22:57:14 2013
5+
** by: The Qt Meta Object Compiler version 63 (Qt 4.8.2)
66
**
77
** WARNING! All changes made in this file will be lost!
88
*****************************************************************************/
@@ -11,7 +11,7 @@
1111
#if !defined(Q_MOC_OUTPUT_REVISION)
1212
#error "The header file 'dialog.h' doesn't include <QObject>."
1313
#elif Q_MOC_OUTPUT_REVISION != 63
14-
#error "This file was generated using the moc from 4.8.1. It"
14+
#error "This file was generated using the moc from 4.8.2. It"
1515
#error "cannot be used with the include files from this version of Qt."
1616
#error "(The moc has changed too much.)"
1717
#endif
@@ -23,24 +23,28 @@ static const uint qt_meta_data_Dialog[] = {
2323
6, // revision
2424
0, // classname
2525
0, 0, // classinfo
26-
4, 14, // methods
26+
6, 14, // methods
2727
0, 0, // properties
2828
0, 0, // enums/sets
2929
0, 0, // constructors
3030
0, // flags
3131
0, // signalCount
3232

3333
// slots: signature, parameters, type, tag, flags
34-
14, 8, 7, 7, 0x08,
35-
56, 7, 7, 7, 0x08,
36-
82, 7, 7, 7, 0x08,
37-
109, 7, 7, 7, 0x08,
34+
8, 7, 7, 7, 0x08,
35+
24, 7, 7, 7, 0x08,
36+
66, 60, 7, 7, 0x08,
37+
108, 7, 7, 7, 0x08,
38+
134, 7, 7, 7, 0x08,
39+
161, 7, 7, 7, 0x08,
3840

3941
0 // eod
4042
};
4143

4244
static const char qt_meta_stringdata_Dialog[] = {
43-
"Dialog\0\0index\0on_tableWidget_doubleClicked(QModelIndex)\0"
45+
"Dialog\0\0operationDone()\0"
46+
"mount_cb(std::function<void(void)>)\0"
47+
"index\0on_tableWidget_doubleClicked(QModelIndex)\0"
4448
"on_button_mount_clicked()\0"
4549
"on_button_umount_clicked()\0"
4650
"on_button_refresh_clicked()\0"
@@ -52,10 +56,12 @@ void Dialog::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void
5256
Q_ASSERT(staticMetaObject.cast(_o));
5357
Dialog *_t = static_cast<Dialog *>(_o);
5458
switch (_id) {
55-
case 0: _t->on_tableWidget_doubleClicked((*reinterpret_cast< QModelIndex(*)>(_a[1]))); break;
56-
case 1: _t->on_button_mount_clicked(); break;
57-
case 2: _t->on_button_umount_clicked(); break;
58-
case 3: _t->on_button_refresh_clicked(); break;
59+
case 0: _t->operationDone(); break;
60+
case 1: _t->mount_cb((*reinterpret_cast< std::function<void(void)>(*)>(_a[1]))); break;
61+
case 2: _t->on_tableWidget_doubleClicked((*reinterpret_cast< QModelIndex(*)>(_a[1]))); break;
62+
case 3: _t->on_button_mount_clicked(); break;
63+
case 4: _t->on_button_umount_clicked(); break;
64+
case 5: _t->on_button_refresh_clicked(); break;
5965
default: ;
6066
}
6167
}
@@ -84,8 +90,6 @@ void *Dialog::qt_metacast(const char *_clname)
8490
if (!_clname) return 0;
8591
if (!strcmp(_clname, qt_meta_stringdata_Dialog))
8692
return static_cast<void*>(const_cast< Dialog*>(this));
87-
if (!strcmp(_clname, "Mount"))
88-
return static_cast< Mount*>(const_cast< Dialog*>(this));
8993
return QDialog::qt_metacast(_clname);
9094
}
9195

@@ -95,9 +99,9 @@ int Dialog::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
9599
if (_id < 0)
96100
return _id;
97101
if (_c == QMetaObject::InvokeMetaMethod) {
98-
if (_id < 4)
102+
if (_id < 6)
99103
qt_static_metacall(this, _c, _id, _a);
100-
_id -= 4;
104+
_id -= 6;
101105
}
102106
return _id;
103107
}

qcryptdisk.pro

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
# -------------------------------------------------
2-
# Project created by QtCreator 2010-09-08T09:05:22
3-
# -------------------------------------------------
4-
TARGET = qcryptdisk
1+
######################################################################
2+
# Automatically generated by qmake (2.01a) ?? ????. 5 22:14:59 2013
3+
######################################################################
4+
55
TEMPLATE = app
6-
SOURCES += main.cpp \
7-
dialog.cpp \
8-
util.cpp
9-
HEADERS += dialog.h \
10-
util.h
6+
TARGET =
7+
DEPENDPATH += .
8+
INCLUDEPATH += .
9+
10+
# Input
11+
HEADERS += dialog.h util.h
1112
FORMS += dialog.ui
13+
SOURCES += dialog.cpp main.cpp util.cpp
14+
QMAKE_CXXFLAGS += -std=c++0x

0 commit comments

Comments
 (0)