6
6
#include " plugin.h"
7
7
#include " ui_configwidget.h"
8
8
#include < QSettings>
9
+ #include < QThread>
9
10
ALBERT_LOGGING_CATEGORY (" qalculate" )
10
11
using namespace albert;
11
12
using namespace std ;
@@ -18,6 +19,8 @@ const char* CFG_PARSINGMODE = "parsing_mode";
18
19
const uint DEF_PARSINGMODE = (int )PARSING_MODE_CONVENTIONAL;
19
20
const char * CFG_PRECISION = " precision" ;
20
21
const uint DEF_PRECISION = 16 ;
22
+ const QStringList icon_urls = {" xdg:calc" , " :qalculate" };
23
+ std::mutex qalculate_mutex;
21
24
}
22
25
23
26
Plugin::Plugin ()
@@ -65,6 +68,7 @@ QWidget *Plugin::buildConfigWidget()
65
68
static_cast <void (QComboBox::*)(int )>(&QComboBox::currentIndexChanged),
66
69
this , [this ](int index){
67
70
settings ()->setValue (CFG_ANGLEUNIT, index);
71
+ std::lock_guard locker (qalculate_mutex);
68
72
eo.parse_options .angle_unit = static_cast <AngleUnit>(index);
69
73
});
70
74
@@ -74,6 +78,7 @@ QWidget *Plugin::buildConfigWidget()
74
78
static_cast <void (QComboBox::*)(int )>(&QComboBox::currentIndexChanged),
75
79
this , [this ](int index){
76
80
settings ()->setValue (CFG_PARSINGMODE, index);
81
+ std::lock_guard locker (qalculate_mutex);
77
82
eo.parse_options .parsing_mode = static_cast <ParsingMode>(index);
78
83
});
79
84
@@ -83,6 +88,7 @@ QWidget *Plugin::buildConfigWidget()
83
88
static_cast <void (QSpinBox::*)(int )>(&QSpinBox::valueChanged),
84
89
this , [this ](int value){
85
90
settings ()->setValue (CFG_PRECISION, value);
91
+ std::lock_guard locker (qalculate_mutex);
86
92
qalc->setPrecision (value);
87
93
});
88
94
@@ -97,10 +103,24 @@ vector<RankItem> Plugin::handleGlobalQuery(const GlobalQuery *query) const
97
103
if (trimmed.isEmpty ())
98
104
return results;
99
105
106
+ std::lock_guard locker (qalculate_mutex);
107
+
100
108
auto expression = qalc->unlocalizeExpression (query->string ().toStdString (), eo.parse_options );
101
- auto mstruct = qalc->calculate (expression, eo);
109
+
110
+ qalc->startControl ();
111
+ MathStructure mstruct;
112
+ qalc->calculate (&mstruct, expression, 0 , eo);
113
+ for (; qalc->busy (); QThread::msleep (10 ))
114
+ if (!query->isValid ())
115
+ qalc->abort ();
116
+ qalc->stopControl ();
117
+
118
+ if (!query->isValid ())
119
+ return results;
120
+
102
121
if (qalc->message ()){
103
- qalc->clearMessages ();
122
+ for (auto msg = qalc->message (); msg; msg = qalc->nextMessage ())
123
+ DEBG << QString::fromUtf8 (qalc->message ()->c_message ());
104
124
return results;
105
125
}
106
126
@@ -113,7 +133,7 @@ vector<RankItem> Plugin::handleGlobalQuery(const GlobalQuery *query) const
113
133
result,
114
134
QString (" %1esult of %2" ).arg (mstruct.isApproximate ()?" Approximate r" :" R" , trimmed),
115
135
result, // TODO if handler finally knows its trigger change this to fire a triggered query
116
- { " xdg:calc " , " :qalculate " } ,
136
+ icon_urls ,
117
137
{
118
138
{
119
139
" cpr" , " Copy result to clipboard" ,
@@ -129,35 +149,46 @@ vector<RankItem> Plugin::handleGlobalQuery(const GlobalQuery *query) const
129
149
return results;
130
150
}
131
151
132
-
133
-
134
152
void Plugin::handleTriggerQuery (TriggerQuery *query) const
135
153
{
136
154
auto trimmed = query->string ().trimmed ();
137
155
if (trimmed.isEmpty ())
138
156
return ;
139
157
158
+ std::lock_guard locker (qalculate_mutex);
159
+
140
160
auto eo_ = eo;
141
161
eo_.parse_options .functions_enabled = true ;
142
162
eo_.parse_options .units_enabled = true ;
143
163
eo_.parse_options .unknowns_enabled = true ;
144
164
145
165
auto expression = qalc->unlocalizeExpression (query->string ().toStdString (), eo_.parse_options );
146
- auto mstruct = qalc->calculate (expression, eo_);
166
+
167
+ qalc->startControl ();
168
+ MathStructure mstruct;
169
+ qalc->calculate (&mstruct, expression, 0 , eo_);
170
+ for (; qalc->busy (); QThread::msleep (10 ))
171
+ if (!query->isValid ())
172
+ qalc->abort ();
173
+ qalc->stopControl ();
174
+
175
+ if (!query->isValid ())
176
+ return ;
177
+
147
178
QStringList errors;
148
179
for (auto msg = qalc->message (); msg; msg = qalc->nextMessage ())
149
180
errors << QString::fromUtf8 (qalc->message ()->c_message ());
150
- mstruct.format (po);
151
181
152
182
if (errors.empty ()){
183
+ mstruct.format (po);
153
184
auto result = QString::fromStdString (mstruct.print (po));
154
185
query->add (
155
186
StandardItem::make (
156
187
" qalc-res" ,
157
188
result,
158
189
QString (" %1esult of %2" ).arg (mstruct.isApproximate ()?" Approximate r" :" R" , trimmed),
159
190
QString (" %1%2" ).arg (query->trigger (), result),
160
- { " xdg:calc " , " :qalculate " } ,
191
+ icon_urls ,
161
192
{
162
193
{
163
194
" cpr" , " Copy result to clipboard" ,
@@ -176,7 +207,7 @@ void Plugin::handleTriggerQuery(TriggerQuery *query) const
176
207
" qalc-err" ,
177
208
" Evaluation error." ,
178
209
errors.join (" " ),
179
- { " xdg:calc " , " :qalculate " } ,
210
+ icon_urls ,
180
211
{{" manual" , " Visit documentation" , [=](){ openUrl (URL_MANUAL); }}}
181
212
)
182
213
);
0 commit comments