@@ -19,6 +19,11 @@ const char* CFG_PARSINGMODE = "parsing_mode";
19
19
const uint DEF_PARSINGMODE = (int )PARSING_MODE_CONVENTIONAL;
20
20
const char * CFG_PRECISION = " precision" ;
21
21
const uint DEF_PRECISION = 16 ;
22
+ const char * CFG_UNITS = " units_in_global_query" ;
23
+ const bool DEF_UNITS = false ;
24
+ const char * CFG_FUNCS = " functions_in_global_query" ;
25
+ const bool DEF_FUNCS = false ;
26
+
22
27
}
23
28
24
29
const QStringList Plugin::icon_urls = {" xdg:calc" , " :qalculate" };
@@ -41,10 +46,10 @@ Plugin::Plugin()
41
46
42
47
// parse options
43
48
eo.parse_options .angle_unit = static_cast <AngleUnit>(s->value (CFG_ANGLEUNIT, DEF_ANGLEUNIT).toInt ());
44
- eo.parse_options .functions_enabled = false ;
49
+ eo.parse_options .functions_enabled = s-> value (CFG_FUNCS, DEF_FUNCS). toBool () ;
45
50
eo.parse_options .limit_implicit_multiplication = true ;
46
51
eo.parse_options .parsing_mode = static_cast <ParsingMode>(s->value (CFG_PARSINGMODE, DEF_PARSINGMODE).toInt ());
47
- eo.parse_options .units_enabled = false ;
52
+ eo.parse_options .units_enabled = s-> value (CFG_UNITS, DEF_UNITS). toBool () ;
48
53
eo.parse_options .unknowns_enabled = false ;
49
54
50
55
// print options
@@ -101,6 +106,24 @@ QWidget *Plugin::buildConfigWidget()
101
106
qalc->setPrecision (value);
102
107
});
103
108
109
+ // Units in global query
110
+ ui.unitsInGlobalQueryCheckBox ->setChecked (eo.parse_options .units_enabled );
111
+ connect (ui.unitsInGlobalQueryCheckBox , &QCheckBox::toggled, this , [this ](bool checked)
112
+ {
113
+ settings ()->setValue (CFG_UNITS, checked);
114
+ lock_guard locker (qalculate_mutex);
115
+ eo.parse_options .units_enabled = checked;
116
+ });
117
+
118
+ // Functions in global query
119
+ ui.functionsInGlobalQueryCheckBox ->setChecked (eo.parse_options .functions_enabled );
120
+ connect (ui.functionsInGlobalQueryCheckBox , &QCheckBox::toggled, this , [this ](bool checked)
121
+ {
122
+ settings ()->setValue (CFG_FUNCS, checked);
123
+ lock_guard locker (qalculate_mutex);
124
+ eo.parse_options .functions_enabled = checked;
125
+ });
126
+
104
127
return widget;
105
128
}
106
129
@@ -125,38 +148,62 @@ shared_ptr<Item> Plugin::buildItem(const QString &query, const MathStructure &ms
125
148
);
126
149
}
127
150
128
- vector<RankItem> Plugin::handleGlobalQuery (const Query *query)
151
+ std::variant<QStringList, MathStructure>
152
+ Plugin::runQalculateLocked (const albert::Query *query, const EvaluationOptions &eo_)
129
153
{
130
- vector<RankItem> results;
131
-
132
- auto trimmed = query->string ().trimmed ();
133
- if (trimmed.isEmpty ())
134
- return results;
135
-
136
- lock_guard locker (qalculate_mutex);
137
-
138
154
auto expression = qalc->unlocalizeExpression (query->string ().toStdString (), eo.parse_options );
139
155
140
156
qalc->startControl ();
141
157
MathStructure mstruct;
142
- qalc->calculate (&mstruct, expression, 0 , eo );
158
+ qalc->calculate (&mstruct, expression, 0 , eo_ );
143
159
for (; qalc->busy (); QThread::msleep (10 ))
144
160
if (!query->isValid ())
145
161
qalc->abort ();
146
162
qalc->stopControl ();
147
163
148
164
if (!query->isValid ())
149
- return results ;
165
+ return QStringList () ;
150
166
151
- if (qalc->message ()){
152
- for (auto msg = qalc->message (); msg; msg = qalc->nextMessage ())
153
- DEBG << QString::fromUtf8 (qalc->message ()->c_message ());
154
- return results;
167
+ QStringList errors;
168
+ for (auto msg = qalc->message (); msg; msg = qalc->nextMessage ())
169
+ errors << QString::fromUtf8 (qalc->message ()->c_message ());
170
+
171
+ if (errors.empty ())
172
+ {
173
+ mstruct.format (po);
174
+ return mstruct;
155
175
}
176
+ else
177
+ return errors;
178
+ }
179
+
180
+ vector<RankItem> Plugin::handleGlobalQuery (const Query *query)
181
+ {
182
+ vector<RankItem> results;
156
183
157
- mstruct.format (po);
184
+ auto trimmed = query->string ().trimmed ();
185
+ if (trimmed.isEmpty ())
186
+ return results;
158
187
159
- results.emplace_back (buildItem (trimmed, mstruct), 1 .0f );
188
+ lock_guard locker (qalculate_mutex);
189
+
190
+ auto ret = runQalculateLocked (query, eo);
191
+
192
+ if (!query->isValid ())
193
+ return results;
194
+
195
+ try {
196
+ auto mstruct = std::get<MathStructure>(ret);
197
+ results.emplace_back (buildItem (trimmed, mstruct), 1 .0f );
198
+ } catch (const std::bad_variant_access &) {
199
+ try {
200
+ auto errors = std::get<QStringList>(ret);
201
+ for (const auto & e : errors)
202
+ DEBG << e;
203
+ } catch (const std::bad_variant_access &) {
204
+ CRIT << " Unhandled bad_variant_access" ;
205
+ }
206
+ }
160
207
161
208
return results;
162
209
}
@@ -167,47 +214,37 @@ void Plugin::handleTriggerQuery(Query *query)
167
214
if (trimmed.isEmpty ())
168
215
return ;
169
216
170
- lock_guard locker (qalculate_mutex);
171
-
172
217
auto eo_ = eo;
173
218
eo_.parse_options .functions_enabled = true ;
174
219
eo_.parse_options .units_enabled = true ;
175
220
eo_.parse_options .unknowns_enabled = true ;
176
221
177
- auto expression = qalc-> unlocalizeExpression (query-> string (). toStdString (), eo_. parse_options );
222
+ lock_guard locker (qalculate_mutex );
178
223
179
- qalc->startControl ();
180
- MathStructure mstruct;
181
- qalc->calculate (&mstruct, expression, 0 , eo_);
182
- for (; qalc->busy (); QThread::msleep (10 ))
183
- if (!query->isValid ())
184
- qalc->abort ();
185
- qalc->stopControl ();
224
+ auto ret = runQalculateLocked (query, eo_);
186
225
187
226
if (!query->isValid ())
188
227
return ;
189
228
190
- QStringList errors;
191
- for (auto msg = qalc->message (); msg; msg = qalc->nextMessage ())
192
- errors << QString::fromUtf8 (qalc->message ()->c_message ());
193
-
194
- if (errors.empty ())
195
- {
196
- mstruct.format (po);
229
+ try {
230
+ auto mstruct = std::get<MathStructure>(ret);
197
231
query->add (buildItem (trimmed, mstruct));
198
- }
199
- else
200
- {
201
- static const auto tr_e = tr (" Evaluation error." );
202
- static const auto tr_d = tr (" Visit documentation" );
203
- query->add (
204
- StandardItem::make (
205
- " qalc-err" ,
206
- tr_e,
207
- errors.join (" " ),
208
- icon_urls,
209
- {{" manual" , tr_d, [=](){ openUrl (URL_MANUAL); }}}
210
- )
211
- );
232
+ } catch (const std::bad_variant_access &) {
233
+ try {
234
+ auto errors = std::get<QStringList>(ret);
235
+ static const auto tr_e = tr (" Evaluation error." );
236
+ static const auto tr_d = tr (" Visit documentation" );
237
+ query->add (
238
+ StandardItem::make (
239
+ " qalc-err" ,
240
+ tr_e,
241
+ errors.join (" , " ),
242
+ icon_urls,
243
+ {{" manual" , tr_d, [=](){ openUrl (URL_MANUAL); }}}
244
+ )
245
+ );
246
+ } catch (const std::bad_variant_access &) {
247
+ CRIT << " Unhandled bad_variant_access" ;
248
+ }
212
249
}
213
250
}
0 commit comments