├── .gitignore ├── AUTHORS ├── LICENSE ├── README.md ├── demo ├── MainWindow.cpp ├── MainWindow.h ├── MainWindow.ui ├── demo.pro ├── main.cpp └── mypixmap.h ├── qt-async-lib.pri ├── qt-async-lib ├── Config.h ├── qt-async-lib.pro ├── third_party │ ├── QtProgressCircle │ │ ├── Demo.cpp │ │ ├── Demo.png │ │ ├── DemoItem.h │ │ ├── DemoWindow.h │ │ ├── DemoWindow.ui │ │ ├── ProgressCircle.cpp │ │ ├── ProgressCircle.h │ │ ├── ProgressCircle.pro │ │ └── README.md │ ├── QtWaitingSpinner │ │ ├── LICENSE │ │ ├── README.md │ │ ├── qtwaitingspinner.pri │ │ ├── qtwaitingspinner.pro │ │ ├── waitingspinnerwidget.cpp │ │ └── waitingspinnerwidget.h │ └── scope_exit.h ├── values │ ├── AsyncError.h │ ├── AsyncProgress.h │ ├── AsyncTrackErrorsPolicy.h │ ├── AsyncValue.h │ ├── AsyncValueBase.cpp │ ├── AsyncValueBase.h │ ├── AsyncValueRunNetwork.h │ ├── AsyncValueRunThread.h │ ├── AsyncValueRunThreadPool.h │ ├── AsyncValueRunable.h │ └── AsyncValueTemplate.h └── widgets │ ├── AsyncWidget.h │ ├── AsyncWidgetBase.h │ ├── AsyncWidgetError.cpp │ ├── AsyncWidgetError.h │ ├── AsyncWidgetProgressBar.cpp │ ├── AsyncWidgetProgressBar.h │ ├── AsyncWidgetProgressCircle.cpp │ ├── AsyncWidgetProgressCircle.h │ ├── AsyncWidgetProgressSpinner.cpp │ ├── AsyncWidgetProgressSpinner.h │ ├── AsyncWidgetProxy.cpp │ └── AsyncWidgetProxy.h ├── qt-async.pro └── tests ├── TestAsyncValue.cpp ├── TestAsyncValue.h ├── main.cpp └── tests.pro /.gitignore: -------------------------------------------------------------------------------- 1 | /tmp/ 2 | /bin/ 3 | /bin-*/ 4 | build-* 5 | /workdir/ 6 | *.pro.user 7 | *~ 8 | *.directory 9 | *.autosave 10 | *Thumbs.db 11 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Alexey Zhondin 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This library has some usefull Qt classes and widgets for asyncronious operations. 2 | 3 | [![qt-async demo video](https://img.youtube.com/vi/aTXOpmVRXq0/maxresdefault.jpg)](https://youtu.be/aTXOpmVRXq0) 4 | 5 | # Brief overview 6 | The library introduces *async values*. Like std::future *async value* holds the *result* of async operation or *error* (if operation was unsuccessfull). 7 | Also it holds *progress* if async operation is still executing and has *stateChanged* signal that fires when async value is switching between progress, result and error. 8 | 9 | 10 | Like `future std::async(Callable fn)` qt-async library has a set of functions `bool asyncValueRunXXX(AsyncValue& value, Callable fn, ProgressArgs... args)` to make asyncronious calculation for the *value*. 11 | All these functions switch *value* to a progress state (and pass *args* to progress constructor) and invoke `fn(progress, value)` so *fn* implementation can use *progress* to report executing progress or ask if execution was requested to stop. 12 | *value* parameter is used to set a result of the operation or an error if calculation has failed. 13 | 14 | 15 | To represent such *async values* in GUI qt-async library has *async widgets*. Every time *async value* state changes, *async widget* creates sub-widget for that state (ProgressWidget, ErrorWidget or ValueWidget). 16 | Similar to *async value* where user should supply 'fn' that actually calculates value, user should supply to *async widget* a factory how to create ValueWidget (it can be overriden virtual function or std::function). 17 | 18 | # Basic use 19 | 20 | Declare async value object that holds values of type QString and initialized with string "Hello World!": 21 | ```C++ 22 | using AsyncQString = AsyncValue; 23 | AsyncQString value(AsyncInitByValue{}, "Hello World!"); 24 | ``` 25 | 26 | When you need to calculate value, call one of the `asyncValueRunXXX` functions: 27 | ```C++ 28 | bool success = asyncValueRunThreadPool(value, [](AsyncProgress& progress, AsyncQString& value) { 29 | 30 | // a long calculations 31 | for (auto i : {0, 1, 2, 3, 4}) 32 | { 33 | // report progress i/5 34 | progress.setProgress(i, 5); 35 | 36 | // check if calculation was stopped 37 | if (progress.isStopRequested()) 38 | { 39 | // report error 40 | value.emplaceError("Stopped"); 41 | return; 42 | } 43 | 44 | QThread::sleep(1); 45 | } 46 | 47 | // do final processing 48 | progress.setProgress(1.f); 49 | progress.setMessage("Processing..."); 50 | QThread::sleep(1); 51 | 52 | QString val = "Loaded value is 42"; 53 | // set calculated value 54 | value.emplaceValue(std::move(val)); 55 | 56 | }, "Loading...", ASYNC_CAN_REQUEST_STOP::YES); 57 | ``` 58 | The available functions are: 59 | * [asyncValueRunThread](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncValueRunThread.h#L23) - creates QThread, does calculations and deletes QThread (don't use this function) 60 | * [asyncValueRunThreadPool](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncValueRunThreadPool.h#L24) - does calculation in a Qt thread pool 61 | * [asyncValueRunNetwork](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncValueRunNetwork.h#L24) - waits QNetworkReply and does calculation from it. 62 | See [runInThread](https://github.com/lexxmark/qt-async/blob/40af2b9e0a07f8d5cae1e62e039c36012b4234d0/tests/TestAsyncValue.cpp#L48) and [runInThreadPool](https://github.com/lexxmark/qt-async/blob/40af2b9e0a07f8d5cae1e62e039c36012b4234d0/tests/TestAsyncValue.cpp#L62) tests for examples. 63 | 64 | Somewhere in GUI code declare async widget: 65 | ```C++ 66 | // create widget 67 | auto valueWidget = new AsyncWidgetFn(parent); 68 | 69 | // set callback that creates sub-widget to show QString value 70 | valueWidget->createValueWidget = [](QString& value, QWidget* parent) { 71 | // create QLabel 72 | return AsyncWidgetProxy::createLabel(value, parent); 73 | }; 74 | 75 | // assign async value with widget 76 | valueWidget->setValue(&value); 77 | ``` 78 | 79 | Instead of callbacks you can derive widget class from [AsyncWidgetBase](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/widgets/AsyncWidgetBase.h#L24) or [AsyncWidget](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/widgets/AsyncWidget.h#L27) classes and override 4 functions: 80 | 81 | ```C++ 82 | // creates widget to show value 83 | virtual QWidget* createValueWidgetImpl(ValueType& value, QWidget* parent); 84 | 85 | // creates widget to show error 86 | virtual QWidget* createErrorWidgetImpl(ErrorType& error, QWidget* parent); 87 | 88 | // creates widget to show progress 89 | virtual QWidget* createProgressWidgetImpl(ProgressType& progress, QWidget* parent); 90 | 91 | // creates widget when no async value has been assigned to async widget 92 | virtual QWidget* createNoAsyncValueWidgetImpl(QWidget* parent); 93 | ``` 94 | 95 | # AsyncValue API 96 | Most of the `AsyncValue` functions can be found in [AsyncValueTemplate<...>](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncValueTemplate.h#L35) base class. 97 | 98 | User can initialize `AsyncValue` using value or error, `AsyncInitByValue` and `AsyncInitByError` tag classes are used to distinguish these two cases: 99 | ```C++ 100 | using AsyncInt = AsyncValue; 101 | 102 | // init by value 103 | AsyncInt value(AsyncInitByValue{}, 12); 104 | // init by parent and value 105 | auto value = new AsyncInt(parent, AsyncInitByValue{}, 12); 106 | // init by error 107 | AsyncInt value(AsyncInitByError{}, "No int available"); 108 | // init by parent and error 109 | auto value = new AsyncInt(parent, AsyncInitByError{}, "No int available"); 110 | ``` 111 | 112 | There is `stateChanged` signal that is emitted when async value's state changes between value, error and progress. NOTE: you cannot modify async value in it's stateChanged signal handler otherwise deadlock will happen. 113 | ```C++ 114 | QObject::connect(value, &AsyncValueBase::stateChanged, [](ASYNC_VALUE_STATE state) { 115 | // async value state change handler 116 | }); 117 | ``` 118 | 119 | To get the content of the async value use `access` functions and supply callables to access either value or error or progress: 120 | ```C++ 121 | // get any content of the async value 122 | value.access([](int value) { /* access int value here */ }, 123 | [](AsyncError& error) { /* access error here */ }, 124 | [](AsyncProgress& progress) { /* access progress here */ }); 125 | 126 | // get value of the async value 127 | bool success = value.accessValue([](int value) { /* access int value here */ }); 128 | ``` 129 | 130 | User can assign a value using the following functions: 131 | ```C++ 132 | AsyncValue value(...); 133 | 134 | // create value by passing value's constructor parameters 135 | value.emplaceValue(5, 'b'); 136 | 137 | // or create value and pass it to async value 138 | auto str = std::make_unique(5, 'b'); 139 | value.moveValue(std::move(str)); 140 | ``` 141 | User can assign an error in a similar way: 142 | ```C++ 143 | AsyncValue value(...); 144 | 145 | // create error by passing error's constructor parameters 146 | value.emplaceError("Some error hapenned"); 147 | 148 | // or create error and pass it to async value 149 | auto err = std::make_unique("Some error hapenned"); 150 | value.moveError(std::move(err)); 151 | ``` 152 | `startProgress` and `completeProgress` functions are used by `asyncValueRunXXX` functions to start and finish progress: 153 | ```C++ 154 | template 155 | bool asyncValueRunThreadPool(QThreadPool *pool, AsyncValueType& value, Func&& func, ProgressArgs&& ...progressArgs) 156 | { 157 | // create progress 158 | auto progress = std::make_unique(std::forward(progressArgs)...); 159 | auto progressPtr = progress.get(); 160 | 161 | // try to switch async value to progress state 162 | if (!value.startProgress(std::move(progress))) 163 | return false; 164 | 165 | QtConcurrent::run(pool, [&value, progressPtr, func = std::forward(func)](){ 166 | SCOPE_EXIT { 167 | // finish progress 168 | value.completeProgress(progressPtr); 169 | }; 170 | 171 | // run calculation 172 | func(*progressPtr, value); 173 | }); 174 | 175 | return true; 176 | } 177 | ``` 178 | 179 | Also user has an ability to wait async value for result: 180 | ```C++ 181 | AsyncValue value(...); 182 | ... 183 | value.wait([](int value) { /* access int value here */ }, 184 | [](AsyncError& error) { /* access error here */ }); 185 | ``` 186 | 187 | # Runnable values 188 | Usually it's more convinient to hide details how value is calculated. 189 | 190 | [AsyncValueRunableAbstract](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncValueRunable.h#L26) and [AsyncValueRunableFn](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncValueRunable.h#L71) classes are used in this case. 191 | ```C++ 192 | using AsyncQPixmap = AsyncValueRunableFn; 193 | AsyncQPixmap value(AsyncInitByError{}, "Select image file path."); 194 | 195 | // set callback to start async value calculation 196 | value.deferFn = [&value](const AsyncQPixmap::RunFnType& fn) { 197 | asyncValueRunThread(value, fn, "Loading image...", ASYNC_CAN_REQUEST_STOP::NO); 198 | }; 199 | // set callback to calculate actual value 200 | value.runFn = [](AsyncProgressRerun& progress, AsyncQPixmap& value) { 201 | 202 | auto url = getImageUrl(); 203 | QImage image(url); 204 | 205 | for (auto i : {0, 1, 2, 3}) 206 | { 207 | if (progress.isRerunRequested()) 208 | { 209 | // exit and retry load image with new url 210 | return ; 211 | } 212 | 213 | progress.setProgress(i, 4); 214 | 215 | // do some heavy work 216 | QThread::sleep(1); 217 | } 218 | 219 | if (image.isNull()) 220 | value.emplaceError(QString("Cannot load image from file '%1'.").arg(url)); 221 | else 222 | value.emplaceValue(QPixmap::fromImage(image)); 223 | }; 224 | ``` 225 | To calculate value user need to call run() method: 226 | ```C++ 227 | value.run(); 228 | ``` 229 | This does more than just calls value calculation in async manner. If previous calculation is not completed yet it tries to stop and rerun calculation. It doesn't block calling thread. Here is a code of the run function: 230 | ```C++ 231 | void run() 232 | { 233 | bool isInProgress = accessProgress([](ProgressType& progress) { 234 | // if we are in progress already -> just request rerun 235 | progress.requestRerun(); 236 | }); 237 | 238 | if (isInProgress) 239 | return; 240 | 241 | // run later 242 | deferFn([this] (ProgressType& progress, ThisType& value) { 243 | 244 | for (;;) 245 | { 246 | // try to calculate value 247 | runFn(progress, value); 248 | // if no rerun was requested -> we are good to exit 249 | if (!progress.resetIfRerunRequested()) 250 | break; 251 | } 252 | 253 | }); 254 | } 255 | ``` 256 | The code is quite straightforward. 257 | 258 | User can use the same widgets to show runnable values in GUI: 259 | ```C++ 260 | auto valueWidget = new AsyncWidgetFn(ui->widget); 261 | 262 | valueWidget->createValueWidget = [](QPixmap& value, QWidget* parent) { 263 | auto label = new QLabel(parent); 264 | label->setAlignment(Qt::AlignCenter); 265 | label->setPixmap(value); 266 | label->setStyleSheet("border: 1px solid black"); 267 | return label; 268 | }; 269 | 270 | valueWidget->setValue(&value); 271 | ``` 272 | # Advanced example 273 | In the [MyPixmap.h](https://github.com/lexxmark/qt-async/blob/master/demo/mypixmap.h) file you can find a complete example how to adopt async values and widgets for your needs. 274 | 275 | Let's say you have some class to load and store QPixmap. Loading image from external url should be done asynchronously. Here we just inherit our class from `AsyncValueRunableAbstract` class and override `deferImpl` and `runImpl` functions. When image url has changed we call `run` to perform image loading in a separate thread: 276 | ```C++ 277 | class MyPixmap : public AsyncValueRunableAbstract 278 | { 279 | public: 280 | // init pixmap with an error 281 | MyPixmap() 282 | : AsyncValueRunableAbstract(AsyncInitByError{}, "Select image file path.") 283 | { 284 | } 285 | 286 | // returns umage url 287 | // threadsafe 288 | QString imageUrl() const 289 | { 290 | QReadLocker locker(&m_urlLock); 291 | return m_imageUrl; 292 | } 293 | 294 | // sets new image url and requests image loading 295 | // threadsafe 296 | void setImageUrl(QString url) 297 | { 298 | // change image url 299 | { 300 | QWriteLocker locker(&m_urlLock); 301 | m_imageUrl = url; 302 | } 303 | 304 | // reload image 305 | run(); 306 | } 307 | 308 | protected: 309 | // hide run from public 310 | using AsyncValueRunableAbstract::run; 311 | 312 | // actual image loading will be performed in a separate thread 313 | void deferImpl(RunFnType&& func) final 314 | { 315 | asyncValueRunThread(*this, func, "Loading image...", ASYNC_CAN_REQUEST_STOP::NO); 316 | } 317 | 318 | // image loading code 319 | void runImpl(ProgressType& progress) final 320 | { 321 | auto url = imageUrl(); 322 | 323 | QImage image(url); 324 | 325 | for (auto i : {0, 1, 2, 3}) 326 | { 327 | if (progress.isRerunRequested()) 328 | { 329 | // exit and retry load image with a new path 330 | return ; 331 | } 332 | 333 | progress.setProgress(i, 4); 334 | 335 | // do some heavy work 336 | QThread::sleep(1); 337 | } 338 | 339 | if (image.isNull()) 340 | emplaceError(QString("Cannot load image from file '%1'.").arg(url)); 341 | else 342 | emplaceValue(QPixmap::fromImage(image)); 343 | } 344 | 345 | private: 346 | mutable QReadWriteLock m_urlLock; 347 | QString m_imageUrl; 348 | }; 349 | ``` 350 | The widget for `MyPixmap` class could be implemented like this: 351 | ```C++ 352 | class MyPixmapWidget : public AsyncWidget 353 | { 354 | public: 355 | MyPixmapWidget(QWidget* parent, MyPixmap* value) 356 | : AsyncWidget(parent) 357 | { 358 | setValue(value); 359 | } 360 | 361 | protected: 362 | // creates QLabel to show QPixmap image 363 | QWidget* createValueWidgetImpl(QPixmap& value, QWidget* parent) final 364 | { 365 | auto label = new QLabel(parent); 366 | label->setAlignment(Qt::AlignCenter); 367 | label->setPixmap(value); 368 | label->setStyleSheet("border: 1px solid black"); 369 | return label; 370 | } 371 | }; 372 | ``` 373 | 374 | In Demo application we have 2nd tab that shows GUI for MyPixmap instance. Once user changes image url using button or editbox, the MyPixmapWidget will change its content to progress widget and show error or image on loading completion. 375 | 376 | # Customizations 377 | All async value classes are inherited from `AsyncValueTemplate` template class: 378 | ```C++ 379 | template 380 | class AsyncValueTemplate : public AsyncValueBase 381 | { 382 | ... 383 | }; 384 | ``` 385 | So users can override all type parameters to better adopt async values to different environments. 386 | 387 | By default `ErrorType_t` parameter is an [AsyncError](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncError.h#L22) class: 388 | ```C++ 389 | class AsyncError 390 | { 391 | public: 392 | AsyncError(QString text) 393 | : m_text(std::move(text)) 394 | {} 395 | 396 | QString text() const { return m_text; } 397 | 398 | private: 399 | QString m_text; 400 | }; 401 | ``` 402 | The `ProgressType_t` parameter represented by [AsyncProgress](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncProgress.h#L29) with the following functions: 403 | ```C++ 404 | // the text describing the current progress 405 | QString message() const; 406 | // current progress position in [0, 1] 407 | float progress() const; 408 | // returns true if progress can handle stop requests 409 | bool canRequestStop() const; 410 | // returns true if progress was requested to stop 411 | bool isStopRequested() const; 412 | 413 | // sets text describing the current progress 414 | void setMessage(QString message); 415 | // sets current progress position in [0, 1] 416 | void setProgress(float progress); 417 | // sets current position using current step and total number of steps 418 | template 419 | void setProgress(Num current, Num total) 420 | { 421 | if (total != 0) 422 | setProgress(static_cast(current) / static_cast(total)); 423 | } 424 | // requests stop of the current progress 425 | void requestStop(); 426 | ``` 427 | 428 | `TrackErrorsPolicy_t` parameter is used to customize reaction to inconsistent or incorrect situations. By default [AsyncTrackErrorsPolicyDefault](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncTrackErrorsPolicy.h#L39) class is used: 429 | ```C++ 430 | struct AsyncTrackErrorsPolicy 431 | { 432 | // class to catch emit deadlocks 433 | struct EmitGuard 434 | { 435 | EmitGuard(AsyncTrackErrorsPolicyNone&) {} 436 | }; 437 | 438 | using EmitGuardType = EmitGuard; 439 | 440 | // called when async value is changing inside stateChanged signal handler 441 | // this incorrect situation will lead to a deadlock 442 | void trackEmitDeadlock() const; 443 | 444 | // called when async value is in progress and destructing at the same time 445 | // usually user should call value.stopAndWait(); before value destruction 446 | void inProgressWhileDestruct() const; 447 | 448 | // called if startProgress is called while value is in progress already 449 | void startProgressWhileInProgress() const; 450 | 451 | // called when completeProgress is called with different progress object 452 | void tryCompleteAlienProgress() const; 453 | 454 | // called inside value.completeProgress function if niether value nor error was assigned to async value 455 | void incompleteProgress() const; 456 | }; 457 | ``` 458 | 459 | To use async values with different asynchronious API or frameworks you can create `asynValueRunXXX` like function. 460 | The schema is simple: 461 | ```C++ 462 | template 463 | bool asyncValueRunMyFramework(AsyncValueType& value, Routine func, ...) 464 | { 465 | // create progress 466 | auto progress = std::make_unique(...); 467 | auto progressPtr = progress.get(); 468 | 469 | // try to switch value to progress state 470 | if (!value.startProgress(std::move(progress))) 471 | return false; 472 | 473 | // move async value and calculation routine to MyFramework 474 | MyFramework::AsyncCall([&value, progressPtr, func = std::forward(func)]() { 475 | // invoke routine 476 | func(...); 477 | 478 | // finalize progress 479 | value.completeProgress(progressPtr); 480 | }); 481 | } 482 | ``` 483 | -------------------------------------------------------------------------------- /demo/MainWindow.cpp: -------------------------------------------------------------------------------- 1 | #include "MainWindow.h" 2 | #include "ui_MainWindow.h" 3 | #include 4 | #include 5 | #include "widgets/AsyncWidget.h" 6 | #include "widgets/AsyncWidgetProgressSpinner.h" 7 | #include "widgets/AsyncWidgetProgressCircle.h" 8 | #include "values/AsyncValueRunThreadPool.h" 9 | #include "values/AsyncValueRunNetwork.h" 10 | 11 | MainWindow::MainWindow(QWidget *parent) : 12 | QMainWindow(parent), 13 | ui(new Ui::MainWindow), 14 | m_value(this, AsyncInitByValue{}, "Hello World!") 15 | { 16 | ui->setupUi(this); 17 | move(QApplication::desktop()->availableGeometry().center() - rect().center()); 18 | 19 | // init pixmap widget 20 | { 21 | auto valueWidget = new MyPixmapWidget(ui->widget, &m_pixmap); 22 | ui->imageAsyncWidget->setContentWidget(valueWidget); 23 | } 24 | 25 | // init string widget 26 | { 27 | auto valueWidget = new AsyncWidgetFn(ui->widget); 28 | 29 | valueWidget->createValueWidget = [](QString& value, QWidget* parent) { 30 | return AsyncWidgetProxy::createLabel(value, parent); 31 | }; 32 | 33 | valueWidget->createProgressWidget = [this](AsyncProgress& progress, QWidget* parent)->QWidget* { 34 | switch (m_progressWidgetMode) { 35 | case PROGRESS_MODE::SPINNER_LINES: 36 | return new AsyncWidgetProgressSpinner(progress, parent); 37 | case PROGRESS_MODE::PROGRESS_CIRCLE: 38 | return new AsyncWidgetProgressCircle(progress, parent); 39 | 40 | default: 41 | return new AsyncWidgetProgressBar(progress, parent); 42 | } 43 | }; 44 | 45 | valueWidget->setValue(&m_value); 46 | 47 | ui->widget->setContentWidget(valueWidget); 48 | } 49 | 50 | QObject::connect(&m_value, &AsyncQString::stateChanged, this, &MainWindow::OnAsyncValueChanged); 51 | } 52 | 53 | MainWindow::~MainWindow() 54 | { 55 | delete ui; 56 | m_value.stopAndWait(); 57 | } 58 | 59 | void MainWindow::OnAsyncValueChanged(ASYNC_VALUE_STATE state) 60 | { 61 | ui->errorBttn->setEnabled(state != ASYNC_VALUE_STATE::PROGRESS); 62 | ui->valueBttn->setEnabled(state != ASYNC_VALUE_STATE::PROGRESS); 63 | ui->stopBttn->setEnabled(state == ASYNC_VALUE_STATE::PROGRESS); 64 | } 65 | 66 | void MainWindow::on_errorBttn_clicked() 67 | { 68 | m_value.emplaceError("Error happened!"); 69 | } 70 | 71 | void MainWindow::on_valueBttn_clicked() 72 | { 73 | m_value.emplaceValue("A new value assigned!"); 74 | } 75 | 76 | void MainWindow::on_startBttn_clicked() 77 | { 78 | asyncValueRunThreadPool(m_value, [](AsyncProgress& progress, AsyncValue& value){ 79 | 80 | for (auto i : {0, 1, 2, 3, 4}) 81 | { 82 | progress.setProgress(i, 5); 83 | if (progress.isStopRequested()) 84 | { 85 | value.emplaceError("Stopped"); 86 | return; 87 | } 88 | 89 | QThread::sleep(1); 90 | } 91 | 92 | progress.setProgress(1.f); 93 | progress.setMessage("Processing..."); 94 | QThread::sleep(1); 95 | 96 | QString val = "Loaded value is 42"; 97 | value.emplaceValue(std::move(val)); 98 | 99 | }, "Loading...", ASYNC_CAN_REQUEST_STOP::YES); 100 | } 101 | 102 | 103 | void MainWindow::on_stopBttn_clicked() 104 | { 105 | m_value.accessProgress([](AsyncProgress& progress){ 106 | progress.requestStop(); 107 | }); 108 | } 109 | 110 | void MainWindow::on_radioButtonProgressBar_clicked() 111 | { 112 | m_progressWidgetMode = PROGRESS_MODE::PROGRESS_BAR; 113 | } 114 | 115 | void MainWindow::on_radioButtonSpinnerLines_clicked() 116 | { 117 | m_progressWidgetMode = PROGRESS_MODE::SPINNER_LINES; 118 | } 119 | 120 | void MainWindow::on_radioButtonCircle_clicked() 121 | { 122 | m_progressWidgetMode = PROGRESS_MODE::PROGRESS_CIRCLE; 123 | } 124 | 125 | void MainWindow::on_imageUrlButton_clicked() 126 | { 127 | auto filePath = QFileDialog::getOpenFileName(this, tr("Open Image"), "", tr("Image Files (*.png *.jpg *.bmp)")); 128 | if (filePath.isNull()) 129 | return; 130 | 131 | ui->imageUrl->setText(filePath); 132 | } 133 | 134 | void MainWindow::on_imageUrl_textChanged(const QString & arg) 135 | { 136 | m_pixmap.setImageUrl(arg); 137 | } 138 | -------------------------------------------------------------------------------- /demo/MainWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include "mypixmap.h" 6 | #include 7 | 8 | namespace Ui { 9 | class MainWindow; 10 | } 11 | 12 | using AsyncQString = AsyncValue; 13 | 14 | class MainWindow : public QMainWindow 15 | { 16 | Q_OBJECT 17 | 18 | public: 19 | explicit MainWindow(QWidget *parent = nullptr); 20 | ~MainWindow(); 21 | 22 | private slots: 23 | void OnAsyncValueChanged(ASYNC_VALUE_STATE state); 24 | void on_errorBttn_clicked(); 25 | void on_valueBttn_clicked(); 26 | void on_startBttn_clicked(); 27 | 28 | void on_stopBttn_clicked(); 29 | 30 | void on_radioButtonProgressBar_clicked(); 31 | void on_radioButtonSpinnerLines_clicked(); 32 | void on_radioButtonCircle_clicked(); 33 | void on_imageUrlButton_clicked(); 34 | void on_imageUrl_textChanged(const QString &arg1); 35 | 36 | private: 37 | Ui::MainWindow *ui; 38 | 39 | AsyncQString m_value; 40 | 41 | enum class PROGRESS_MODE 42 | { 43 | PROGRESS_BAR, 44 | SPINNER_LINES, 45 | PROGRESS_CIRCLE, 46 | } m_progressWidgetMode = PROGRESS_MODE::PROGRESS_BAR; 47 | 48 | MyPixmap m_pixmap; 49 | }; 50 | 51 | #endif // MAINWINDOW_H 52 | -------------------------------------------------------------------------------- /demo/MainWindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | true 7 | 8 | 9 | 10 | 0 11 | 0 12 | 741 13 | 511 14 | 15 | 16 | 17 | 18 | 10 19 | 20 | 21 | 22 | Async Demo 23 | 24 | 25 | 26 | 24 27 | 24 28 | 29 | 30 | 31 | 32 | 33 | 0 34 | 0 35 | 36 | 37 | 38 | 39 | 40 | 41 | 0 42 | 43 | 44 | 45 | IntegerAsyncValue 46 | 47 | 48 | 49 | 50 | 51 | QLayout::SetMinimumSize 52 | 53 | 54 | 55 | 56 | 57 | 0 58 | 0 59 | 60 | 61 | 62 | Set Error 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 0 71 | 0 72 | 73 | 74 | 75 | Set Value 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 0 84 | 0 85 | 86 | 87 | 88 | Start 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 0 97 | 0 98 | 99 | 100 | 101 | Stop 102 | 103 | 104 | 105 | 106 | 107 | 108 | Progress UI 109 | 110 | 111 | 112 | 113 | 114 | Progress Bar 115 | 116 | 117 | true 118 | 119 | 120 | 121 | 122 | 123 | 124 | Spinner Lines 125 | 126 | 127 | 128 | 129 | 130 | 131 | Progress Circle 132 | 133 | 134 | 135 | 136 | 137 | 138 | false 139 | 140 | 141 | Spinner Dots 142 | 143 | 144 | 145 | 146 | 147 | 148 | false 149 | 150 | 151 | Flying Dots 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | Qt::Vertical 162 | 163 | 164 | QSizePolicy::Expanding 165 | 166 | 167 | 168 | 20 169 | 40 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 0 181 | 0 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | ImageAsyncValueRunable 191 | 192 | 193 | 194 | 195 | 196 | 197 | 0 198 | 0 199 | 200 | 201 | 202 | 203 | 204 | 205 | Choose file 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 0 214 | 0 215 | 216 | 217 | 218 | 219 | 200 220 | 0 221 | 222 | 223 | 224 | enter image path 225 | 226 | 227 | 228 | 229 | 230 | 231 | Qt::Vertical 232 | 233 | 234 | 235 | 20 236 | 364 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | AsyncWidgetProxy 258 | QWidget 259 |
widgets/AsyncWidgetProxy.h
260 | 1 261 |
262 |
263 | 264 | 265 |
266 | -------------------------------------------------------------------------------- /demo/demo.pro: -------------------------------------------------------------------------------- 1 | CONFIG += c++14 2 | 3 | QT += core gui widgets network 4 | QT += concurrent 5 | 6 | TARGET = qt-async-demo 7 | TEMPLATE = app 8 | 9 | SOURCES += main.cpp\ 10 | MainWindow.cpp 11 | 12 | HEADERS += MainWindow.h \ 13 | mypixmap.h 14 | 15 | FORMS += MainWindow.ui 16 | 17 | INCLUDEPATH += ../qt-async-lib 18 | 19 | win32 { 20 | CONFIG(debug, debug|release): ASYNC_LIB_PATH = $$OUT_PWD/../qt-async-lib/debug 21 | CONFIG(release, debug|release): ASYNC_LIB_PATH = $$OUT_PWD/../qt-async-lib/release 22 | } else:unix { 23 | ASYNC_LIB_PATH = $$OUT_PWD/../qt-async-lib 24 | } 25 | 26 | LIBS += -L$$ASYNC_LIB_PATH -lqt-async-lib 27 | 28 | win32:PRE_TARGETDEPS += $$ASYNC_LIB_PATH/qt-async-lib.lib 29 | -------------------------------------------------------------------------------- /demo/main.cpp: -------------------------------------------------------------------------------- 1 | #include "MainWindow.h" 2 | #include 3 | #include 4 | 5 | static QString asyncStyleSheet = R"( 6 | AsyncWidgetError, 7 | AsyncWidgetProgressBar, 8 | AsyncWidgetProgressSpinner, 9 | AsyncWidgetProgressCircle, 10 | QLabel#AsyncLabel { 11 | background: white; 12 | border: 1px solid black; 13 | } 14 | )"; 15 | 16 | int main(int argc, char *argv[]) 17 | { 18 | //QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 19 | 20 | QApplication a(argc, argv); 21 | 22 | a.setStyleSheet(asyncStyleSheet); 23 | MainWindow w; 24 | w.show(); 25 | 26 | return a.exec(); 27 | } 28 | -------------------------------------------------------------------------------- /demo/mypixmap.h: -------------------------------------------------------------------------------- 1 | #ifndef MYPIXMAP_H 2 | #define MYPIXMAP_H 3 | 4 | #include "values/AsyncValueRunable.h" 5 | #include "values/AsyncValueRunThread.h" 6 | #include "widgets/AsyncWidget.h" 7 | #include 8 | 9 | class MyPixmap : public AsyncValueRunableAbstract 10 | { 11 | public: 12 | MyPixmap() 13 | : AsyncValueRunableAbstract(AsyncInitByError{}, "Select image file path.") 14 | { 15 | } 16 | 17 | QString imageUrl() const 18 | { 19 | QReadLocker locker(&m_urlLock); 20 | return m_imageUrl; 21 | } 22 | 23 | void setImageUrl(QString url) 24 | { 25 | { 26 | QWriteLocker locker(&m_urlLock); 27 | m_imageUrl = url; 28 | } 29 | 30 | // reload image 31 | run(); 32 | } 33 | 34 | protected: 35 | // hide run from public 36 | using AsyncValueRunableAbstract::run; 37 | 38 | void deferImpl(RunFnType&& func) final 39 | { 40 | asyncValueRunThread(*this, func, "Loading image...", ASYNC_CAN_REQUEST_STOP::NO); 41 | } 42 | 43 | void runImpl(ProgressType& progress) final 44 | { 45 | auto url = imageUrl(); 46 | 47 | QImage image(url); 48 | 49 | for (auto i : {0, 1, 2, 3}) 50 | { 51 | if (progress.isRerunRequested()) 52 | { 53 | // exit and retry load image with new path 54 | return ; 55 | } 56 | 57 | progress.setProgress(i, 4); 58 | 59 | // do some heavy work 60 | QThread::sleep(1); 61 | } 62 | 63 | if (image.isNull()) 64 | emplaceError(QString("Cannot load image from file '%1'.").arg(url)); 65 | else 66 | emplaceValue(QPixmap::fromImage(image)); 67 | } 68 | 69 | private: 70 | mutable QReadWriteLock m_urlLock; 71 | QString m_imageUrl; 72 | }; 73 | 74 | class MyPixmapWidget : public AsyncWidget 75 | { 76 | public: 77 | MyPixmapWidget(QWidget* parent, MyPixmap* value) 78 | : AsyncWidget(parent) 79 | { 80 | setValue(value); 81 | } 82 | 83 | protected: 84 | QWidget* createValueWidgetImpl(ValueType& value, QWidget* parent) final 85 | { 86 | auto label = new QLabel(parent); 87 | label->setAlignment(Qt::AlignCenter); 88 | label->setPixmap(value); 89 | label->setStyleSheet("border: 1px solid black"); 90 | return label; 91 | } 92 | }; 93 | 94 | #endif // MYPIXMAP_H 95 | -------------------------------------------------------------------------------- /qt-async-lib.pri: -------------------------------------------------------------------------------- 1 | CONFIG += qtn_unity_build 2 | QT += script widgets 3 | 4 | QTNPROPVERSION = 1.0.0 5 | 6 | INCLUDEPATH += $$PWD $$PWD/Core $$PWD/PropertyWidget 7 | 8 | VPATH += $$PWD/Core $$PWD/PropertyWidget 9 | 10 | SOURCES_CORE += PropertyBase.cpp \ 11 | Property.cpp \ 12 | PropertySet.cpp \ 13 | Enum.cpp \ 14 | QObjectPropertySet.cpp \ 15 | Core/PropertyBool.cpp \ 16 | Core/PropertyInt.cpp \ 17 | Core/PropertyUInt.cpp \ 18 | Core/PropertyFloat.cpp \ 19 | Core/PropertyDouble.cpp \ 20 | Core/PropertyQString.cpp \ 21 | Core/PropertyQRect.cpp \ 22 | Core/PropertyEnum.cpp \ 23 | Core/PropertyEnumFlags.cpp \ 24 | Core/PropertyQSize.cpp \ 25 | Core/PropertyQPoint.cpp \ 26 | GUI/PropertyQColor.cpp \ 27 | GUI/PropertyButton.cpp \ 28 | GUI/PropertyQPen.cpp \ 29 | GUI/PropertyQBrush.cpp \ 30 | GUI/PropertyQFont.cpp 31 | 32 | SOURCES_WIDGET += PropertyWidget.cpp \ 33 | PropertyView.cpp \ 34 | Utils/InplaceEditing.cpp \ 35 | Delegates/PropertyDelegateFactory.cpp \ 36 | Delegates/Core/PropertyDelegateBool.cpp \ 37 | Delegates/Core/PropertyDelegateInt.cpp \ 38 | Delegates/Core/PropertyDelegateUInt.cpp \ 39 | Delegates/Core/PropertyDelegateQString.cpp \ 40 | Delegates/Core/PropertyDelegateFloat.cpp \ 41 | Delegates/Core/PropertyDelegateDouble.cpp \ 42 | Delegates/Core/PropertyDelegateEnum.cpp \ 43 | Delegates/Core/PropertyDelegateQRect.cpp \ 44 | Delegates/Utils/PropertyEditorHandler.cpp \ 45 | Delegates/Core/PropertyDelegateEnumFlags.cpp \ 46 | Delegates/PropertyDelegate.cpp \ 47 | Delegates/PropertyDelegateAux.cpp \ 48 | Delegates/Utils/PropertyDelegatePropertySet.cpp \ 49 | Delegates/Utils/PropertyDelegateSliderBox.cpp \ 50 | Delegates/Utils/PropertyDelegateMisc.cpp \ 51 | Delegates/Utils/PropertyEditorAux.cpp \ 52 | Delegates/Core/PropertyDelegateQSize.cpp \ 53 | Delegates/Core/PropertyDelegateQPoint.cpp \ 54 | Delegates/GUI/PropertyDelegateQFont.cpp \ 55 | Delegates/GUI/PropertyDelegateQColor.cpp \ 56 | Delegates/GUI/PropertyDelegateButton.cpp \ 57 | Delegates/GUI/PropertyDelegateQPen.cpp \ 58 | Delegates/GUI/PropertyDelegateQBrush.cpp \ 59 | Utils/AccessibilityProxy.cpp 60 | 61 | qtn_unity_build: SOURCES += $$PWD/QtnPropertyUnity.cpp 62 | else: SOURCES += $$SOURCES_CORE $$SOURCES_WIDGET 63 | 64 | HEADERS += CoreAPI.h\ 65 | PropertyBase.h \ 66 | Property.h\ 67 | PropertySet.h\ 68 | Enum.h\ 69 | QObjectPropertySet.h \ 70 | PropertyCore.h \ 71 | PropertyGUI.h \ 72 | Auxiliary/PropertyTemplates.h \ 73 | Auxiliary/PropertyMacro.h \ 74 | Auxiliary/PropertyAux.h \ 75 | Auxiliary/PropertyDelegateInfo.h \ 76 | Core/PropertyBool.h \ 77 | Core/PropertyInt.h \ 78 | Core/PropertyUInt.h \ 79 | Core/PropertyFloat.h \ 80 | Core/PropertyDouble.h \ 81 | Core/PropertyQString.h \ 82 | Core/PropertyQRect.h \ 83 | Core/PropertyEnum.h \ 84 | Core/PropertyEnumFlags.h \ 85 | Core/PropertyQSize.h \ 86 | Core/PropertyQPoint.h \ 87 | GUI/PropertyQColor.h \ 88 | GUI/PropertyButton.h \ 89 | GUI/PropertyQPen.h \ 90 | GUI/PropertyQBrush.h \ 91 | GUI/PropertyQFont.h 92 | 93 | HEADERS += PropertyWidgetAPI.h \ 94 | PropertyWidget.h \ 95 | PropertyView.h \ 96 | Utils/InplaceEditing.h \ 97 | Delegates/PropertyDelegate.h \ 98 | Delegates/Utils/PropertyDelegatePropertySet.h \ 99 | Delegates/Utils/PropertyDelegateSliderBox.h \ 100 | Delegates/Utils/PropertyDelegateMisc.h \ 101 | Delegates/PropertyDelegateAux.h \ 102 | Delegates/PropertyDelegateFactory.h \ 103 | Delegates/Core/PropertyDelegateBool.h \ 104 | Delegates/Core/PropertyDelegateInt.h \ 105 | Delegates/Core/PropertyDelegateUInt.h \ 106 | Delegates/Core/PropertyDelegateQString.h \ 107 | Delegates/Core/PropertyDelegateFloat.h \ 108 | Delegates/Core/PropertyDelegateDouble.h \ 109 | Delegates/Core/PropertyDelegateEnum.h \ 110 | Delegates/Core/PropertyDelegateQRect.h \ 111 | Delegates/Utils/PropertyEditorHandler.h \ 112 | Delegates/Core/PropertyDelegateEnumFlags.h \ 113 | Delegates/Utils/PropertyEditorAux.h \ 114 | Delegates/Core/PropertyDelegateQSize.h \ 115 | Delegates/Core/PropertyDelegateQPoint.h \ 116 | Delegates/GUI/PropertyDelegateQFont.h \ 117 | Delegates/GUI/PropertyDelegateQColor.h \ 118 | Delegates/GUI/PropertyDelegateButton.h \ 119 | Delegates/GUI/PropertyDelegateQPen.h \ 120 | Delegates/GUI/PropertyDelegateQBrush.h \ 121 | Utils/AccessibilityProxy.h 122 | -------------------------------------------------------------------------------- /qt-async-lib/Config.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_CONFIG_H 18 | #define ASYNC_CONFIG_H 19 | 20 | #define ASYNC_PROGRESS_WIDGET_UPDATE_TIMEOUT 200 21 | 22 | #endif // ASYNC_CONFIG_H 23 | -------------------------------------------------------------------------------- /qt-async-lib/qt-async-lib.pro: -------------------------------------------------------------------------------- 1 | CONFIG += staticlib 2 | CONFIG += c++14 3 | 4 | TARGET = qt-async-lib 5 | TEMPLATE = lib 6 | VERSION = 1.0.0 7 | 8 | QT += widgets concurrent network 9 | QT -= gui 10 | 11 | SOURCES += \ 12 | values/AsyncValueBase.cpp \ 13 | widgets/AsyncWidgetProxy.cpp \ 14 | widgets/AsyncWidgetError.cpp \ 15 | widgets/AsyncWidgetProgressBar.cpp \ 16 | widgets/AsyncWidgetProgressSpinner.cpp \ 17 | widgets/AsyncWidgetProgressCircle.cpp \ 18 | third_party/QtWaitingSpinner/waitingspinnerwidget.cpp \ 19 | third_party/QtProgressCircle/ProgressCircle.cpp 20 | 21 | HEADERS += \ 22 | Config.h \ 23 | values/AsyncValueBase.h \ 24 | values/AsyncValueTemplate.h \ 25 | values/AsyncError.h \ 26 | values/AsyncProgress.h \ 27 | values/AsyncValue.h \ 28 | values/AsyncValueRunThreadPool.h \ 29 | values/AsyncTrackErrorsPolicy.h \ 30 | values/AsyncValueRunThread.h \ 31 | values/AsyncValueRunable.h \ 32 | values/AsyncValueRunNetwork.h \ 33 | widgets/AsyncWidgetProxy.h \ 34 | widgets/AsyncWidgetBase.h \ 35 | widgets/AsyncWidget.h \ 36 | widgets/AsyncWidgetError.h \ 37 | widgets/AsyncWidgetProgressBar.h \ 38 | widgets/AsyncWidgetProgressSpinner.h \ 39 | widgets/AsyncWidgetProgressCircle.h \ 40 | third_party/scope_exit.h \ 41 | third_party/QtWaitingSpinner/waitingspinnerwidget.h \ 42 | third_party/QtProgressCircle/ProgressCircle.h 43 | 44 | DEFINES += QT_DEPRECATED_WARNINGS 45 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtProgressCircle/Demo.cpp: -------------------------------------------------------------------------------- 1 | #include "DemoWindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | DemoWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtProgressCircle/Demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lexxmark/qt-async/8196fc9d54e111df66e41aa7741bf06315ad3374/qt-async-lib/third_party/QtProgressCircle/Demo.png -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtProgressCircle/DemoItem.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ProgressCircle.h" 4 | #include 5 | #include 6 | #include 7 | 8 | class DemoItem : public QWidget 9 | { 10 | public: 11 | explicit DemoItem(QString text, QWidget * parent = 0) : 12 | QWidget(parent) 13 | { 14 | setStyleSheet(".QWidget{border-bottom: 1px solid lightgrey;}"); 15 | QHBoxLayout * layout = new QHBoxLayout(this); 16 | layout->setSpacing(12); 17 | progressCircle = new ProgressCircle(); 18 | progressCircle->setFixedSize(30, 30); 19 | layout->addWidget(progressCircle); 20 | label = new QLabel(text); 21 | layout->addWidget(label, 1); 22 | QPushButton * closeButton = new QPushButton(tr("Delete")); 23 | connect(closeButton, &QPushButton::clicked, this, &QObject::deleteLater); 24 | layout->addWidget(closeButton); 25 | 26 | QPropertyAnimation * progressCircleAnimation = new QPropertyAnimation(progressCircle, "outerRadius", progressCircle); 27 | progressCircleAnimation->setDuration(750); 28 | progressCircleAnimation->setEasingCurve(QEasingCurve::OutQuad); 29 | progressCircleAnimation->setStartValue(0.0); 30 | progressCircleAnimation->setEndValue(1.0); 31 | progressCircleAnimation->start(QAbstractAnimation::DeleteWhenStopped); 32 | } 33 | 34 | void finish(bool success) 35 | { 36 | QPropertyAnimation * animation = new QPropertyAnimation(progressCircle, "outerRadius", progressCircle); 37 | animation->setDuration(1500); 38 | animation->setEasingCurve(QEasingCurve::OutQuad); 39 | animation->setEndValue(0.5); 40 | animation->start(QAbstractAnimation::DeleteWhenStopped); 41 | 42 | animation = new QPropertyAnimation(progressCircle, "innerRadius", progressCircle); 43 | animation->setDuration(750); 44 | animation->setEasingCurve(QEasingCurve::OutQuad); 45 | animation->setEndValue(0.0); 46 | animation->start(QAbstractAnimation::DeleteWhenStopped); 47 | 48 | QColor color = success ? QColor(155,219,58) : QColor(255,100,100); 49 | 50 | animation = new QPropertyAnimation(progressCircle, "color", progressCircle); 51 | animation->setDuration(750); 52 | animation->setEasingCurve(QEasingCurve::OutQuad); 53 | animation->setEndValue(color); 54 | animation->start(QAbstractAnimation::DeleteWhenStopped); 55 | 56 | label->setText(success ? "Successfully finished" : "Aborted"); 57 | } 58 | 59 | ProgressCircle * progressCircle; 60 | 61 | private: 62 | QLabel * label; 63 | }; 64 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtProgressCircle/DemoWindow.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ui_DemoWindow.h" 4 | #include "DemoItem.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | class DemoWindow : public QMainWindow 14 | { 15 | Q_OBJECT 16 | 17 | public: 18 | explicit DemoWindow(QWidget *parent = 0) : 19 | QMainWindow(parent), 20 | ui(new Ui::DemoWindow) 21 | { 22 | ui->setupUi(this); 23 | ui->itemListLayout->addStretch(); 24 | 25 | connect(ui->indefiniteButton, &QPushButton::clicked, this, &DemoWindow::addIndefinite); 26 | connect(ui->successfulButton, &QPushButton::clicked, this, &DemoWindow::addSuccessful); 27 | connect(ui->abortedButton, &QPushButton::clicked, this, &DemoWindow::addAborted); 28 | } 29 | 30 | public slots: 31 | void addIndefinite() 32 | { 33 | DemoItem * item = new DemoItem("Indefinite process"); 34 | ui->itemListLayout->insertWidget(0, item); 35 | } 36 | 37 | void addSuccessful() 38 | { 39 | DemoItem * item = new DemoItem("Working"); 40 | ui->itemListLayout->insertWidget(0, item); 41 | item->progressCircle->setMaximum(100); 42 | 43 | QTimer::singleShot(500, item, [=](){ 44 | item->progressCircle->setValue(10); 45 | }); 46 | 47 | QTimer::singleShot(1000, item, [=](){ 48 | item->progressCircle->setValue(30); 49 | }); 50 | 51 | QTimer::singleShot(2000, item, [=](){ 52 | item->progressCircle->setValue(60); 53 | }); 54 | 55 | QTimer::singleShot(2500, item, [=](){ 56 | item->progressCircle->setValue(70); 57 | }); 58 | 59 | QTimer::singleShot(3300, item, [=](){ 60 | item->progressCircle->setValue(85); 61 | }); 62 | 63 | QTimer::singleShot(4000, item, [=](){ 64 | item->progressCircle->setValue(100); 65 | item->finish(true); 66 | }); 67 | } 68 | 69 | void addAborted() 70 | { 71 | DemoItem * item = new DemoItem("Working"); 72 | ui->itemListLayout->insertWidget(0, item); 73 | item->progressCircle->setMaximum(100); 74 | 75 | QTimer::singleShot(500, item, [=](){ 76 | item->progressCircle->setValue(10); 77 | }); 78 | 79 | QTimer::singleShot(1000, item, [=](){ 80 | item->progressCircle->setValue(30); 81 | }); 82 | 83 | QTimer::singleShot(1500, item, [=](){ 84 | item->progressCircle->setValue(65); 85 | }); 86 | 87 | QTimer::singleShot(2000, item, [=](){ 88 | item->finish(false); 89 | }); 90 | } 91 | 92 | private: 93 | QScopedPointer ui; 94 | }; 95 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtProgressCircle/DemoWindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | DemoWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | Progress Circle Demo 15 | 16 | 17 | 18 | 19 | 12 20 | 21 | 22 | 23 | 24 | 0 25 | 26 | 27 | 28 | 29 | 30 | 0 31 | 32 32 | 33 | 34 | 35 | Indefinite 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 0 44 | 32 45 | 46 | 47 | 48 | Successful 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 0 57 | 32 58 | 59 | 60 | 61 | Aborted 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | #itemList{ 71 | background-color: white; 72 | border-radius: 4px; 73 | border: 1px solid lightgrey; 74 | } 75 | 76 | 77 | 78 | 0 79 | 80 | 81 | 0 82 | 83 | 84 | 0 85 | 86 | 87 | 0 88 | 89 | 90 | 91 | 92 | 0 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtProgressCircle/ProgressCircle.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Date of creation: 04.07.2013 3 | // Creator: Alexander Egorov aka mofr 4 | // Authors: mofr 5 | ///////////////////////////////////////////////////////////////////////////// 6 | 7 | #include "ProgressCircle.h" 8 | #include 9 | #include 10 | 11 | ProgressCircle::ProgressCircle(QWidget *parent) : 12 | QWidget(parent), 13 | mValue(0), 14 | mMaximum(0), 15 | mInnerRadius(0.6), 16 | mOuterRadius(1.0), 17 | mColor(110,190,235), 18 | mVisibleValue(0), 19 | mValueAnimation(this, "visibleValue"), 20 | mInfiniteAnimation(this, "infiniteAnimationValue"), 21 | mInfiniteAnimationValue(0.0) 22 | { 23 | mInfiniteAnimation.setLoopCount(-1);//infinite 24 | mInfiniteAnimation.setDuration(1000); 25 | mInfiniteAnimation.setStartValue(0.0); 26 | mInfiniteAnimation.setEndValue(1.0); 27 | mInfiniteAnimation.start(); 28 | } 29 | 30 | int ProgressCircle::value() const 31 | { 32 | return mValue; 33 | } 34 | 35 | int ProgressCircle::maximum() const 36 | { 37 | return mMaximum; 38 | } 39 | 40 | qreal ProgressCircle::innerRadius() const 41 | { 42 | return mInnerRadius; 43 | } 44 | 45 | qreal ProgressCircle::outerRadius() const 46 | { 47 | return mOuterRadius; 48 | } 49 | 50 | QColor ProgressCircle::color() const 51 | { 52 | return mColor; 53 | } 54 | 55 | void ProgressCircle::setValue(int value) 56 | { 57 | if(value < 0) value = 0; 58 | 59 | if(mValue != value) 60 | { 61 | mValueAnimation.stop(); 62 | mValueAnimation.setEndValue(value); 63 | mValueAnimation.setDuration(250); 64 | mValueAnimation.start(); 65 | 66 | mValue = value; 67 | emit valueChanged(value); 68 | } 69 | } 70 | 71 | void ProgressCircle::setMaximum(int maximum) 72 | { 73 | if(maximum < 0) maximum = 0; 74 | 75 | if(mMaximum != maximum) 76 | { 77 | mMaximum = maximum; 78 | update(); 79 | emit maximumChanged(maximum); 80 | 81 | if(mMaximum == 0) 82 | { 83 | mInfiniteAnimation.start(); 84 | } 85 | else 86 | { 87 | mInfiniteAnimation.stop(); 88 | } 89 | } 90 | } 91 | 92 | void ProgressCircle::setInnerRadius(qreal innerRadius) 93 | { 94 | if(innerRadius > 1.0) innerRadius = 1.0; 95 | if(innerRadius < 0.0) innerRadius = 0.0; 96 | 97 | if(mInnerRadius != innerRadius) 98 | { 99 | mInnerRadius = innerRadius; 100 | update(); 101 | } 102 | } 103 | 104 | void ProgressCircle::setOuterRadius(qreal outerRadius) 105 | { 106 | if(outerRadius > 1.0) outerRadius = 1.0; 107 | if(outerRadius < 0.0) outerRadius = 0.0; 108 | 109 | if(mOuterRadius != outerRadius) 110 | { 111 | mOuterRadius = outerRadius; 112 | update(); 113 | } 114 | } 115 | 116 | void ProgressCircle::setColor(QColor color) 117 | { 118 | if(color != mColor) 119 | { 120 | mColor = color; 121 | update(); 122 | } 123 | } 124 | 125 | QRectF squared(QRectF rect) 126 | { 127 | if(rect.width() > rect.height()) 128 | { 129 | qreal diff = rect.width() - rect.height(); 130 | return rect.adjusted(diff/2, 0, -diff/2, 0); 131 | } 132 | else 133 | { 134 | qreal diff = rect.height() - rect.width(); 135 | return rect.adjusted(0, diff/2, 0, -diff/2); 136 | } 137 | } 138 | 139 | void ProgressCircle::paintEvent(QPaintEvent *) 140 | { 141 | QPixmap pixmap; 142 | if (!QPixmapCache::find(key(), pixmap)) 143 | { 144 | pixmap = generatePixmap(); 145 | QPixmapCache::insert(key(), pixmap); 146 | } 147 | 148 | // Draw pixmap at center of item 149 | QPainter painter(this); 150 | painter.drawPixmap( 0.5 * ( width() - pixmap.width() ), 0.5 * ( height() - pixmap.height() ), pixmap ); 151 | } 152 | 153 | void ProgressCircle::setInfiniteAnimationValue(qreal value) 154 | { 155 | mInfiniteAnimationValue = value; 156 | update(); 157 | } 158 | 159 | void ProgressCircle::setVisibleValue(int value) 160 | { 161 | if(mVisibleValue != value) 162 | { 163 | mVisibleValue = value; 164 | update(); 165 | } 166 | } 167 | 168 | QString ProgressCircle::key() const 169 | { 170 | return QString("%1,%2,%3,%4,%5,%6,%7,%8") 171 | .arg(mInfiniteAnimationValue) 172 | .arg(mVisibleValue) 173 | .arg(mMaximum) 174 | .arg(mInnerRadius) 175 | .arg(mOuterRadius) 176 | .arg(width()) 177 | .arg(height()) 178 | .arg(mColor.rgb()) 179 | ; 180 | } 181 | 182 | QPixmap ProgressCircle::generatePixmap() const 183 | { 184 | QPixmap pixmap(squared(rect()).size().toSize()); 185 | pixmap.fill(QColor(0,0,0,0)); 186 | QPainter painter(&pixmap); 187 | 188 | painter.setRenderHint(QPainter::Antialiasing, true); 189 | 190 | QRectF rect = pixmap.rect().adjusted(1,1,-1,-1); 191 | qreal margin = rect.width()*(1.0 - mOuterRadius)/2.0; 192 | rect.adjust(margin,margin,-margin,-margin); 193 | qreal innerRadius = mInnerRadius*rect.width()/2.0; 194 | 195 | //background grey circle 196 | painter.setBrush(QColor(225,225,225)); 197 | painter.setPen(QColor(225,225,225)); 198 | painter.drawPie(rect, 0, 360*16); 199 | 200 | painter.setBrush(mColor); 201 | painter.setPen(mColor); 202 | 203 | if(mMaximum == 0) 204 | { 205 | //draw as infinite process 206 | int startAngle = -mInfiniteAnimationValue * 360 * 16; 207 | int spanAngle = 0.15 * 360 * 16; 208 | painter.drawPie(rect, startAngle, spanAngle); 209 | } 210 | else 211 | { 212 | int value = qMin(mVisibleValue, mMaximum); 213 | int startAngle = 90 * 16; 214 | int spanAngle = -qreal(value) * 360 * 16 / mMaximum; 215 | 216 | painter.drawPie(rect, startAngle, spanAngle); 217 | } 218 | 219 | //inner circle and frame 220 | painter.setBrush(QColor(255,255,255)); 221 | painter.setPen(QColor(0,0,0, 60)); 222 | painter.drawEllipse(rect.center(), innerRadius, innerRadius); 223 | 224 | //outer frame 225 | painter.drawArc(rect, 0, 360*16); 226 | 227 | return pixmap; 228 | } 229 | 230 | qreal ProgressCircle::infiniteAnimationValue() const 231 | { 232 | return mInfiniteAnimationValue; 233 | } 234 | 235 | int ProgressCircle::visibleValue() const 236 | { 237 | return mVisibleValue; 238 | } 239 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtProgressCircle/ProgressCircle.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Date of creation: 04.07.2013 3 | // Creator: Alexander Egorov aka mofr 4 | // Authors: mofr 5 | ///////////////////////////////////////////////////////////////////////////// 6 | 7 | #pragma once 8 | 9 | #include 10 | #include 11 | 12 | class ProgressCircle : public QWidget 13 | { 14 | Q_OBJECT 15 | Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged) 16 | Q_PROPERTY(int maximum READ maximum WRITE setMaximum NOTIFY maximumChanged) 17 | Q_PROPERTY(qreal innerRadius READ innerRadius WRITE setInnerRadius) 18 | Q_PROPERTY(qreal outerRadius READ outerRadius WRITE setOuterRadius) 19 | Q_PROPERTY(QColor color READ color WRITE setColor) 20 | 21 | //private 22 | Q_PROPERTY(qreal infiniteAnimationValue READ infiniteAnimationValue WRITE setInfiniteAnimationValue) 23 | Q_PROPERTY(int visibleValue READ visibleValue WRITE setVisibleValue) 24 | 25 | public: 26 | explicit ProgressCircle(QWidget *parent = 0); 27 | 28 | int value() const; 29 | 30 | /** 31 | * @brief maximum 32 | * If maximum <= 0, widget shows infinite process; 33 | * @return 34 | */ 35 | int maximum() const; 36 | 37 | /** 38 | * @brief innerRadius = [0.0 .. 1.0] 39 | * @return 40 | */ 41 | qreal innerRadius() const; 42 | 43 | /** 44 | * @brief outerRadius = [0.0 .. 1.0] 45 | * @return 46 | */ 47 | qreal outerRadius() const; 48 | 49 | QColor color() const; 50 | 51 | public slots: 52 | void setValue(int value); 53 | void setMaximum(int maximum); 54 | 55 | void setInnerRadius(qreal innerRadius); 56 | void setOuterRadius(qreal outerRadius); 57 | 58 | void setColor(QColor color); 59 | 60 | signals: 61 | void valueChanged(int); 62 | void maximumChanged(int); 63 | 64 | protected: 65 | void paintEvent(QPaintEvent *); 66 | 67 | private slots: 68 | void setInfiniteAnimationValue(qreal value); 69 | void setVisibleValue(int value); 70 | 71 | private: 72 | QString key() const; 73 | QPixmap generatePixmap() const; 74 | qreal infiniteAnimationValue() const; 75 | int visibleValue() const; 76 | 77 | private: 78 | //actual current value 79 | int mValue; 80 | int mMaximum; 81 | qreal mInnerRadius; 82 | qreal mOuterRadius; 83 | QColor mColor; 84 | 85 | //value visible to user 86 | int mVisibleValue; 87 | QPropertyAnimation mValueAnimation; 88 | 89 | QPropertyAnimation mInfiniteAnimation; 90 | qreal mInfiniteAnimationValue; 91 | }; 92 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtProgressCircle/ProgressCircle.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2015-09-04T13:18:18 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = ProgressCircle 12 | TEMPLATE = app 13 | 14 | QMAKE_CXXFLAGS += -std=c++11 15 | 16 | SOURCES +=\ 17 | ProgressCircle.cpp \ 18 | Demo.cpp 19 | 20 | HEADERS += \ 21 | ProgressCircle.h \ 22 | DemoItem.h \ 23 | DemoWindow.h 24 | 25 | FORMS += \ 26 | DemoWindow.ui 27 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtProgressCircle/README.md: -------------------------------------------------------------------------------- 1 | # QtProgressCircle 2 | ProgressCircle is circular progress bar widget for Qt 3 | 4 | ![alt tag](https://raw.githubusercontent.com/mofr/QtProgressCircle/master/Demo.png) 5 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtWaitingSpinner/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Original Work Copyright (c) 2012-2015 Alexander Turkin 4 | Modified 2014 by William Hallatt 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | this software and associated documentation files (the "Software"), to deal in 8 | the Software without restriction, including without limitation the rights to 9 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | the Software, and to permit persons to whom the Software is furnished to do so, 11 | subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtWaitingSpinner/README.md: -------------------------------------------------------------------------------- 1 | [![Qt Pods](http://qt-pods.org/assets/logo.png "Qt Pods")](http://qt-pods.org) 2 | 3 | QtWaitingSpinner 4 | ================ 5 | 6 | QtWaitingSpinner is a highly configurable, custom Qt widget for showing "waiting" or "loading" spinner icons in Qt applications, e.g. the spinners below are all QtWaitingSpinner widgets differing only in their configuration: 7 | 8 | ![waiting spinner](https://github.com/snowwlex/QtWaitingSpinner/blob/gh-pages/waiting-spinners.gif) 9 | 10 | ###Configuration 11 | 12 | The following properties can all be controlled directly through their corresponding setters: 13 | 14 | * Colour of the widget 15 | * "Roundness" of the lines 16 | * Speed (rotations per second) 17 | * Number of lines to be drawn 18 | * Line length 19 | * Line width 20 | * Radius of the spinner's "dead space" or inner circle 21 | * The percentage fade of the "trail" 22 | * The minimum opacity of the "trail" 23 | 24 | ###Usage 25 | 26 | Despite being highly configurable, QtWaitingSpinner is extremely easy to use and, to make things even easier, the "QtWaitingSpinnerTest" application can assist you in determining the exact shape, size and colour you'd like your spinner to have. 27 | 28 | For example, the embedded spinner in the QtWaitingSpinnerTest screenshot below can be created as follows: 29 | 30 | ``` 31 | QtWaitingSpinner* spinner = new QtWaitingSpinner(this); 32 | 33 | spinner->setRoundness(70.0); 34 | spinner->setMinimumTrailOpacity(15.0); 35 | spinner->setTrailFadePercentage(70.0); 36 | spinner->setNumberOfLines(12); 37 | spinner->setLineLength(10); 38 | spinner->setLineWidth(5); 39 | spinner->setInnerRadius(10); 40 | spinner->setRevolutionsPerSecond(1); 41 | spinner->setColor(QColor(81, 4, 71)); 42 | 43 | spinner->start(); // gets the show on the road! 44 | 45 | ``` 46 | 47 | ![test dialog](https://github.com/snowwlex/QtWaitingSpinner/blob/gh-pages/test-dialog.png) 48 | 49 | As an alternative example, the code below will create a spinner that (1) blocks all user input to the main application for as long as the spinner is active, (2) automatically centres itself on its parent widget every time "start" is called and (3) makes use of the default shape, size and colour settings. 50 | 51 | ``` 52 | QtWaitingSpinner* spinner = new QtWaitingSpinner(this, Qt::ApplicationModal, true); 53 | spinner->start(); // starts spinning 54 | ``` 55 | 56 | Please use [use this link](https://github.com/snowwlex/QtWaitingSpinner/issues) for feedback, requests or issues. 57 | 58 | Enjoy! 59 | 60 | ###qt-pods 61 | 62 | QtWaitingSpinner is available as a pod within qt-pods. See here for reference: 63 | https://github.com/cybercatalyst/qt-pods 64 | 65 | ###Thanks 66 | 67 | QtWaitingSpinner was inspired by the [spin.js](http://fgnass.github.io/spin.js/) project. 68 | 69 | 70 | ### Forks 71 | There is also [QtWaitingSpinner for Python!](https://github.com/z3ntu/QtWaitingSpinner) Thanks to [z3ntu](https://github.com/z3ntu) for porting it. 72 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtWaitingSpinner/qtwaitingspinner.pri: -------------------------------------------------------------------------------- 1 | INCLUDEPATH += \ 2 | $$PWD 3 | 4 | LIBS += \ 5 | -L../qtwaitingspinner -lqtwaitingspinner 6 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtWaitingSpinner/qtwaitingspinner.pro: -------------------------------------------------------------------------------- 1 | QT += widgets 2 | 3 | TEMPLATE = lib 4 | CONFIG += staticlib 5 | TARGET = qtwaitingspinner 6 | 7 | SOURCES += \ 8 | waitingspinnerwidget.cpp 9 | 10 | HEADERS += \ 11 | waitingspinnerwidget.h 12 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtWaitingSpinner/waitingspinnerwidget.cpp: -------------------------------------------------------------------------------- 1 | /* Original Work Copyright (c) 2012-2014 Alexander Turkin 2 | Modified 2014 by William Hallatt 3 | Modified 2015 by Jacob Dawid 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | // Own includes 24 | #include "waitingspinnerwidget.h" 25 | 26 | // Standard includes 27 | #include 28 | #include 29 | 30 | // Qt includes 31 | #include 32 | #include 33 | 34 | WaitingSpinnerWidget::WaitingSpinnerWidget(QWidget *parent, 35 | bool centerOnParent, 36 | bool disableParentWhenSpinning) 37 | : QWidget(parent), 38 | _centerOnParent(centerOnParent), 39 | _disableParentWhenSpinning(disableParentWhenSpinning) { 40 | initialize(); 41 | } 42 | 43 | WaitingSpinnerWidget::WaitingSpinnerWidget(Qt::WindowModality modality, 44 | QWidget *parent, 45 | bool centerOnParent, 46 | bool disableParentWhenSpinning) 47 | : QWidget(parent, Qt::Dialog | Qt::FramelessWindowHint), 48 | _centerOnParent(centerOnParent), 49 | _disableParentWhenSpinning(disableParentWhenSpinning){ 50 | initialize(); 51 | 52 | // We need to set the window modality AFTER we've hidden the 53 | // widget for the first time since changing this property while 54 | // the widget is visible has no effect. 55 | setWindowModality(modality); 56 | setAttribute(Qt::WA_TranslucentBackground); 57 | } 58 | 59 | void WaitingSpinnerWidget::initialize() { 60 | _color = Qt::black; 61 | _textColor = Qt::black; 62 | _roundness = 100.0; 63 | _minimumTrailOpacity = 3.14159265358979323846; 64 | _trailFadePercentage = 80.0; 65 | _revolutionsPerSecond = 1.57079632679489661923; 66 | _numberOfLines = 20; 67 | _lineLength = 10; 68 | _lineWidth = 2; 69 | _innerRadius = 10; 70 | _currentCounter = 0; 71 | _isSpinning = false; 72 | 73 | _timer = new QTimer(this); 74 | connect(_timer, SIGNAL(timeout()), this, SLOT(rotate())); 75 | updateSize(); 76 | updateTimer(); 77 | hide(); 78 | } 79 | 80 | void WaitingSpinnerWidget::paintEvent(QPaintEvent *) { 81 | updatePosition(); 82 | QPainter painter(this); 83 | painter.fillRect(this->rect(), Qt::transparent); 84 | painter.setRenderHint(QPainter::Antialiasing, true); 85 | 86 | if (_currentCounter >= _numberOfLines) { 87 | _currentCounter = 0; 88 | } 89 | 90 | painter.setPen(Qt::NoPen); 91 | for (int i = 0; i < _numberOfLines; ++i) { 92 | painter.save(); 93 | painter.translate(_innerRadius + _lineLength, 94 | _innerRadius + _lineLength); 95 | painter.translate((width() - _imageSize.width()) / 2, 0); 96 | qreal rotateAngle = 97 | static_cast(360 * i) / static_cast(_numberOfLines); 98 | painter.rotate(rotateAngle); 99 | painter.translate(_innerRadius, 0); 100 | int distance = 101 | lineCountDistanceFromPrimary(i, _currentCounter, _numberOfLines); 102 | QColor color = 103 | currentLineColor(distance, _numberOfLines, _trailFadePercentage, 104 | _minimumTrailOpacity, _color); 105 | painter.setBrush(color); 106 | // TODO improve the way rounded rect is painted 107 | painter.drawRoundedRect( 108 | QRect(0, -_lineWidth / 2, _lineLength, _lineWidth), _roundness, 109 | _roundness, Qt::RelativeSize); 110 | painter.restore(); 111 | } 112 | 113 | if (!_text.isEmpty()) { 114 | painter.setPen(QPen(_textColor)); 115 | painter.drawText(QRect(0, _imageSize.height(), width(), height() - _imageSize.height()), 116 | Qt::AlignBottom | Qt::AlignHCenter, _text); 117 | } 118 | } 119 | 120 | void WaitingSpinnerWidget::start() { 121 | updatePosition(); 122 | _isSpinning = true; 123 | show(); 124 | 125 | if(parentWidget() && _disableParentWhenSpinning) { 126 | parentWidget()->setEnabled(false); 127 | } 128 | 129 | if (!_timer->isActive()) { 130 | _timer->start(); 131 | _currentCounter = 0; 132 | } 133 | } 134 | 135 | void WaitingSpinnerWidget::stop() { 136 | _isSpinning = false; 137 | hide(); 138 | 139 | if(parentWidget() && _disableParentWhenSpinning) { 140 | parentWidget()->setEnabled(true); 141 | } 142 | 143 | if (_timer->isActive()) { 144 | _timer->stop(); 145 | _currentCounter = 0; 146 | } 147 | } 148 | 149 | void WaitingSpinnerWidget::setNumberOfLines(int lines) { 150 | _numberOfLines = lines; 151 | _currentCounter = 0; 152 | updateTimer(); 153 | } 154 | 155 | void WaitingSpinnerWidget::setLineLength(int length) { 156 | _lineLength = length; 157 | updateSize(); 158 | } 159 | 160 | void WaitingSpinnerWidget::setLineWidth(int width) { 161 | _lineWidth = width; 162 | updateSize(); 163 | } 164 | 165 | void WaitingSpinnerWidget::setInnerRadius(int radius) { 166 | _innerRadius = radius; 167 | updateSize(); 168 | } 169 | 170 | void WaitingSpinnerWidget::setText(QString text) { 171 | _text = text; 172 | updateSize(); 173 | } 174 | 175 | QColor WaitingSpinnerWidget::color() { 176 | return _color; 177 | } 178 | 179 | QColor WaitingSpinnerWidget::textColor() { 180 | return _textColor; 181 | } 182 | 183 | QString WaitingSpinnerWidget::text() { 184 | return _text; 185 | } 186 | 187 | qreal WaitingSpinnerWidget::roundness() { 188 | return _roundness; 189 | } 190 | 191 | qreal WaitingSpinnerWidget::minimumTrailOpacity() { 192 | return _minimumTrailOpacity; 193 | } 194 | 195 | qreal WaitingSpinnerWidget::trailFadePercentage() { 196 | return _trailFadePercentage; 197 | } 198 | 199 | qreal WaitingSpinnerWidget::revolutionsPersSecond() { 200 | return _revolutionsPerSecond; 201 | } 202 | 203 | int WaitingSpinnerWidget::numberOfLines() { 204 | return _numberOfLines; 205 | } 206 | 207 | int WaitingSpinnerWidget::lineLength() { 208 | return _lineLength; 209 | } 210 | 211 | int WaitingSpinnerWidget::lineWidth() { 212 | return _lineWidth; 213 | } 214 | 215 | int WaitingSpinnerWidget::innerRadius() { 216 | return _innerRadius; 217 | } 218 | 219 | bool WaitingSpinnerWidget::isSpinning() const { 220 | return _isSpinning; 221 | } 222 | 223 | void WaitingSpinnerWidget::setRoundness(qreal roundness) { 224 | _roundness = std::max(0.0, std::min(100.0, roundness)); 225 | } 226 | 227 | void WaitingSpinnerWidget::setColor(QColor color) { 228 | _color = color; 229 | } 230 | 231 | void WaitingSpinnerWidget::setTextColor(QColor color) { 232 | _textColor = color; 233 | } 234 | 235 | void WaitingSpinnerWidget::setRevolutionsPerSecond(qreal revolutionsPerSecond) { 236 | _revolutionsPerSecond = revolutionsPerSecond; 237 | updateTimer(); 238 | } 239 | 240 | void WaitingSpinnerWidget::setTrailFadePercentage(qreal trail) { 241 | _trailFadePercentage = trail; 242 | } 243 | 244 | void WaitingSpinnerWidget::setMinimumTrailOpacity(qreal minimumTrailOpacity) { 245 | _minimumTrailOpacity = minimumTrailOpacity; 246 | } 247 | 248 | void WaitingSpinnerWidget::rotate() { 249 | ++_currentCounter; 250 | if (_currentCounter >= _numberOfLines) { 251 | _currentCounter = 0; 252 | } 253 | update(); 254 | } 255 | 256 | void WaitingSpinnerWidget::updateSize() { 257 | int size = (_innerRadius + _lineLength) * 2; 258 | _imageSize = QSize(size, size); 259 | if (_text.isEmpty()) { 260 | setFixedSize(size, size); 261 | } else { 262 | QFontMetrics fm(font()); 263 | QSize textSize = QSize(fm.width(_text), fm.height()); 264 | setFixedSize(std::max(size, textSize.width()), size + size / 4 + textSize.height()); 265 | } 266 | } 267 | 268 | void WaitingSpinnerWidget::updateTimer() { 269 | _timer->setInterval(1000 / (_numberOfLines * _revolutionsPerSecond)); 270 | } 271 | 272 | void WaitingSpinnerWidget::updatePosition() { 273 | if (parentWidget() && _centerOnParent) { 274 | move(parentWidget()->width() / 2 - width() / 2, 275 | parentWidget()->height() / 2 - height() / 2); 276 | } 277 | } 278 | 279 | int WaitingSpinnerWidget::lineCountDistanceFromPrimary(int current, int primary, 280 | int totalNrOfLines) { 281 | int distance = primary - current; 282 | if (distance < 0) { 283 | distance += totalNrOfLines; 284 | } 285 | return distance; 286 | } 287 | 288 | QColor WaitingSpinnerWidget::currentLineColor(int countDistance, int totalNrOfLines, 289 | qreal trailFadePerc, qreal minOpacity, 290 | QColor color) { 291 | if (countDistance == 0) { 292 | return color; 293 | } 294 | const qreal minAlphaF = minOpacity / 100.0; 295 | int distanceThreshold = 296 | static_cast(ceil((totalNrOfLines - 1) * trailFadePerc / 100.0)); 297 | if (countDistance > distanceThreshold) { 298 | color.setAlphaF(minAlphaF); 299 | } else { 300 | qreal alphaDiff = color.alphaF() - minAlphaF; 301 | qreal gradient = alphaDiff / static_cast(distanceThreshold + 1); 302 | qreal resultAlpha = color.alphaF() - gradient * countDistance; 303 | 304 | // If alpha is out of bounds, clip it. 305 | resultAlpha = std::min(1.0, std::max(0.0, resultAlpha)); 306 | color.setAlphaF(resultAlpha); 307 | } 308 | return color; 309 | } 310 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/QtWaitingSpinner/waitingspinnerwidget.h: -------------------------------------------------------------------------------- 1 | /* Original Work Copyright (c) 2012-2014 Alexander Turkin 2 | Modified 2014 by William Hallatt 3 | Modified 2015 by Jacob Dawid 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | this software and associated documentation files (the "Software"), to deal in 6 | the Software without restriction, including without limitation the rights to 7 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | the Software, and to permit persons to whom the Software is furnished to do so, 9 | subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | */ 21 | 22 | #pragma once 23 | 24 | // Qt includes 25 | #include 26 | #include 27 | #include 28 | 29 | class WaitingSpinnerWidget : public QWidget { 30 | Q_OBJECT 31 | public: 32 | /*! Constructor for "standard" widget behaviour - use this 33 | * constructor if you wish to, e.g. embed your widget in another. */ 34 | WaitingSpinnerWidget(QWidget *parent = 0, 35 | bool centerOnParent = true, 36 | bool disableParentWhenSpinning = true); 37 | 38 | /*! Constructor - use this constructor to automatically create a modal 39 | * ("blocking") spinner on top of the calling widget/window. If a valid 40 | * parent widget is provided, "centreOnParent" will ensure that 41 | * QtWaitingSpinner automatically centres itself on it, if not, 42 | * "centreOnParent" is ignored. */ 43 | WaitingSpinnerWidget(Qt::WindowModality modality, 44 | QWidget *parent = 0, 45 | bool centerOnParent = true, 46 | bool disableParentWhenSpinning = true); 47 | 48 | public slots: 49 | void start(); 50 | void stop(); 51 | 52 | public: 53 | void setColor(QColor color); 54 | void setTextColor(QColor color); 55 | void setRoundness(qreal roundness); 56 | void setMinimumTrailOpacity(qreal minimumTrailOpacity); 57 | void setTrailFadePercentage(qreal trail); 58 | void setRevolutionsPerSecond(qreal revolutionsPerSecond); 59 | void setNumberOfLines(int lines); 60 | void setLineLength(int length); 61 | void setLineWidth(int width); 62 | void setInnerRadius(int radius); 63 | void setText(QString text); 64 | 65 | QColor color(); 66 | QColor textColor(); 67 | QString text(); 68 | qreal roundness(); 69 | qreal minimumTrailOpacity(); 70 | qreal trailFadePercentage(); 71 | qreal revolutionsPersSecond(); 72 | int numberOfLines(); 73 | int lineLength(); 74 | int lineWidth(); 75 | int innerRadius(); 76 | 77 | bool isSpinning() const; 78 | 79 | private slots: 80 | void rotate(); 81 | 82 | protected: 83 | void paintEvent(QPaintEvent *paintEvent); 84 | 85 | private: 86 | static int lineCountDistanceFromPrimary(int current, int primary, 87 | int totalNrOfLines); 88 | static QColor currentLineColor(int distance, int totalNrOfLines, 89 | qreal trailFadePerc, qreal minOpacity, 90 | QColor color); 91 | 92 | void initialize(); 93 | void updateSize(); 94 | void updateTimer(); 95 | void updatePosition(); 96 | 97 | private: 98 | QColor _color; 99 | qreal _roundness; // 0..100 100 | qreal _minimumTrailOpacity; 101 | qreal _trailFadePercentage; 102 | qreal _revolutionsPerSecond; 103 | int _numberOfLines; 104 | int _lineLength; 105 | int _lineWidth; 106 | int _innerRadius; 107 | QString _text; 108 | QSize _imageSize; 109 | QColor _textColor; 110 | 111 | private: 112 | WaitingSpinnerWidget(const WaitingSpinnerWidget&); 113 | WaitingSpinnerWidget& operator=(const WaitingSpinnerWidget&); 114 | 115 | QTimer *_timer; 116 | bool _centerOnParent; 117 | bool _disableParentWhenSpinning; 118 | int _currentCounter; 119 | bool _isSpinning; 120 | }; 121 | -------------------------------------------------------------------------------- /qt-async-lib/third_party/scope_exit.h: -------------------------------------------------------------------------------- 1 | // code taken from https://github.com/charlesnicholson/scope_exit 2 | // this code is in the public domain. see the LICENSE file for details. 3 | // Simple implementation of Andrei Alexandrescu's scope guard, from one of his C++ talks. 4 | #pragma once 5 | 6 | #include 7 | 8 | namespace detail { 9 | 10 | template< typename Lambda > class scope_exit 11 | { 12 | public: 13 | explicit scope_exit(Lambda lambda); 14 | scope_exit(scope_exit&& lambda); 15 | ~scope_exit(); 16 | scope_exit() = delete; 17 | scope_exit(scope_exit const&) = delete; 18 | scope_exit &operator =(scope_exit const&) = delete; 19 | private: 20 | Lambda lambda_; 21 | bool active_; 22 | }; 23 | 24 | template< typename Lambda > scope_exit< Lambda >::scope_exit(Lambda lambda) 25 | : lambda_(std::move(lambda)) 26 | , active_(true) 27 | { 28 | } 29 | 30 | template< typename Lambda > scope_exit< Lambda >::scope_exit(scope_exit&& rhs) 31 | : lambda_(std::move(rhs.lambda_)) 32 | , active_(rhs.active_) 33 | { 34 | rhs.active_ = false; 35 | } 36 | 37 | template< typename Lambda > scope_exit< Lambda >::~scope_exit() 38 | { 39 | if (active_) { 40 | lambda_(); 41 | } 42 | } 43 | 44 | template< typename Lambda > scope_exit< Lambda > make_scope_exit(Lambda l) 45 | { 46 | return scope_exit< Lambda >(std::move(l)); 47 | } 48 | 49 | enum class scope_exit_placeholder {}; 50 | 51 | template< typename Lambda > scope_exit< Lambda > operator +(scope_exit_placeholder, Lambda&& l) 52 | { 53 | return make_scope_exit< Lambda >(std::forward< Lambda >(l)); 54 | } 55 | 56 | } 57 | 58 | #define SCOPE_EXIT_CONCAT_INNER(x, y) x##y 59 | #define SCOPE_EXIT_CONCAT(x, y) SCOPE_EXIT_CONCAT_INNER(x, y) 60 | 61 | #define SCOPE_EXIT \ 62 | auto SCOPE_EXIT_CONCAT(scope_exit_, __LINE__) = ::detail::scope_exit_placeholder() + [&]() 63 | 64 | -------------------------------------------------------------------------------- /qt-async-lib/values/AsyncError.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_ERROR_H 18 | #define ASYNC_ERROR_H 19 | 20 | #include 21 | 22 | class AsyncError 23 | { 24 | public: 25 | AsyncError(QString text) 26 | : m_text(std::move(text)) 27 | {} 28 | 29 | QString text() const { return m_text; } 30 | 31 | private: 32 | QString m_text; 33 | }; 34 | 35 | #endif // ASYNC_ERROR_H 36 | -------------------------------------------------------------------------------- /qt-async-lib/values/AsyncProgress.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_PROGRESS_H 18 | #define ASYNC_PROGRESS_H 19 | 20 | #include 21 | #include 22 | 23 | enum class ASYNC_CAN_REQUEST_STOP 24 | { 25 | YES, 26 | NO 27 | }; 28 | 29 | class AsyncProgress 30 | { 31 | Q_DISABLE_COPY(AsyncProgress) 32 | 33 | public: 34 | AsyncProgress(QString message, ASYNC_CAN_REQUEST_STOP canRequestStop) 35 | : m_message(std::move(message)), 36 | m_canRequestStop(canRequestStop) 37 | {} 38 | 39 | ~AsyncProgress() 40 | { 41 | #ifdef QT_DEBUG 42 | Q_ASSERT(!m_isInUse && "Progress is still used."); 43 | #endif 44 | } 45 | 46 | QString message() const { QReadLocker locker(&m_lock); return m_message; } 47 | float progress() const { QReadLocker locker(&m_lock); return m_progress; } 48 | bool canRequestStop() const { QReadLocker locker(&m_lock); return m_canRequestStop == ASYNC_CAN_REQUEST_STOP::YES; } 49 | bool isStopRequested() const { QReadLocker locker(&m_lock); return m_isStopRequested; } 50 | 51 | void setMessage(QString message) { QWriteLocker locker(&m_lock); m_message = std::move(message); } 52 | void setProgress(float progress) { QWriteLocker locker(&m_lock); m_progress = progress; } 53 | template 54 | void setProgress(Num current, Num total) 55 | { 56 | if (total != 0) 57 | setProgress(static_cast(current) / static_cast(total)); 58 | } 59 | void requestStop() { QWriteLocker locker(&m_lock); m_isStopRequested = true; } 60 | 61 | #ifdef QT_DEBUG 62 | bool isInUse() const { QReadLocker locker(&m_lock); return m_isInUse; } 63 | void setInUse(bool inUse) { QWriteLocker locker(&m_lock); m_isInUse = inUse; } 64 | #endif 65 | 66 | protected: 67 | mutable QReadWriteLock m_lock; 68 | 69 | QString m_message; 70 | float m_progress = 0.f; 71 | ASYNC_CAN_REQUEST_STOP m_canRequestStop = ASYNC_CAN_REQUEST_STOP::YES; 72 | bool m_isStopRequested = false; 73 | 74 | #ifdef QT_DEBUG 75 | bool m_isInUse = false; 76 | #endif 77 | }; 78 | 79 | class AsyncProgressRerun : public AsyncProgress 80 | { 81 | Q_DISABLE_COPY(AsyncProgressRerun) 82 | 83 | public: 84 | using AsyncProgress::AsyncProgress; 85 | 86 | ~AsyncProgressRerun() 87 | { 88 | Q_ASSERT(!m_isRerunRequested && "Rerun had been requested but not resolved"); 89 | } 90 | 91 | bool isRerunRequested() const 92 | { 93 | QReadLocker locker(&m_lock); 94 | return m_isRerunRequested; 95 | } 96 | 97 | void requestRerun() 98 | { 99 | QWriteLocker locker(&m_lock); 100 | 101 | m_isRerunRequested = true; 102 | m_isStopRequested = true; 103 | } 104 | 105 | bool resetIfRerunRequested() 106 | { 107 | QWriteLocker locker(&m_lock); 108 | 109 | if (!m_isRerunRequested) 110 | return false; 111 | 112 | m_isStopRequested = false; 113 | m_isRerunRequested = false; 114 | 115 | return true; 116 | } 117 | 118 | protected: 119 | bool m_isRerunRequested = false; 120 | }; 121 | 122 | #endif // ASYNC_PROGRESS_H 123 | -------------------------------------------------------------------------------- /qt-async-lib/values/AsyncTrackErrorsPolicy.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_TRACK_ERRORS_POLICY_H 18 | #define ASYNC_TRACK_ERRORS_POLICY_H 19 | 20 | #include 21 | #include 22 | 23 | struct AsyncTrackErrorsPolicyNone 24 | { 25 | struct EmitGuard 26 | { 27 | EmitGuard(AsyncTrackErrorsPolicyNone&) {} 28 | }; 29 | 30 | using EmitGuardType = EmitGuard; 31 | 32 | void trackEmitDeadlock() const {} 33 | void inProgressWhileDestruct() const {} 34 | void startProgressWhileInProgress() const {} 35 | void tryCompleteAlienProgress() const {} 36 | void incompleteProgress() const {} 37 | }; 38 | 39 | struct AsyncTrackErrorsPolicyDefault 40 | { 41 | struct EmitGuard 42 | { 43 | EmitGuard(AsyncTrackErrorsPolicyDefault& owner) 44 | : m_owner(owner) 45 | { 46 | Q_ASSERT(!m_owner.m_emitThread && "Cannot emit while emitting"); 47 | // save emitting thread 48 | m_owner.m_emitThread = QThread::currentThread(); 49 | } 50 | 51 | ~EmitGuard() 52 | { 53 | // reset emitting thread 54 | m_owner.m_emitThread = nullptr; 55 | } 56 | 57 | private: 58 | AsyncTrackErrorsPolicyDefault& m_owner; 59 | }; 60 | 61 | using EmitGuardType = EmitGuard; 62 | void trackEmitDeadlock() const 63 | { 64 | if (m_emitThread == QThread::currentThread()) 65 | throw std::logic_error("Async value deadlock"); 66 | } 67 | 68 | void inProgressWhileDestruct() const 69 | { 70 | Q_ASSERT(false && "Destructing value while it's in progress"); 71 | } 72 | 73 | void startProgressWhileInProgress() const 74 | { 75 | Q_ASSERT(false && "Cannot start new progress while in progress"); 76 | } 77 | 78 | void tryCompleteAlienProgress() const 79 | { 80 | Q_ASSERT(false && "Started progress differs with the progress passed to completeProgress"); 81 | } 82 | 83 | [[noreturn]] void incompleteProgress() const 84 | { 85 | throw std::logic_error("Neither value no error was assigned at the progress stop"); 86 | } 87 | 88 | private: 89 | QThread* m_emitThread = nullptr; 90 | }; 91 | 92 | #endif // ASYNC_TRACK_ERRORS_POLICY_H 93 | -------------------------------------------------------------------------------- /qt-async-lib/values/AsyncValue.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_VALUE_H 18 | #define ASYNC_VALUE_H 19 | 20 | #include "AsyncValueTemplate.h" 21 | #include "AsyncError.h" 22 | #include "AsyncProgress.h" 23 | 24 | template 25 | using AsyncValue = AsyncValueTemplate; 26 | 27 | #endif // ASYNC_VALUE_H 28 | -------------------------------------------------------------------------------- /qt-async-lib/values/AsyncValueBase.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "AsyncValueBase.h" 18 | #include 19 | 20 | static auto async_value_state_type_id = qRegisterMetaType("ASYNC_VALUE_STATE"); 21 | 22 | AsyncValueBase::AsyncValueBase(ASYNC_VALUE_STATE state, QObject* parent) 23 | : QObject(parent), 24 | m_writeLock(QMutex::NonRecursive), 25 | m_contentLock(QReadWriteLock::NonRecursive), 26 | m_state(state) 27 | { 28 | } 29 | 30 | -------------------------------------------------------------------------------- /qt-async-lib/values/AsyncValueBase.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_VALUE_BASE_H 18 | #define ASYNC_VALUE_BASE_H 19 | 20 | #include "../Config.h" 21 | #include "../third_party/scope_exit.h" 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | enum class ASYNC_VALUE_STATE 29 | { 30 | VALUE, 31 | ERROR, 32 | PROGRESS 33 | }; 34 | Q_DECLARE_METATYPE(ASYNC_VALUE_STATE); 35 | 36 | class AsyncValueBase : public QObject 37 | { 38 | Q_OBJECT 39 | Q_DISABLE_COPY(AsyncValueBase) 40 | 41 | signals: 42 | void stateChanged(ASYNC_VALUE_STATE state); 43 | 44 | protected: 45 | explicit AsyncValueBase(ASYNC_VALUE_STATE state, QObject* parent = nullptr); 46 | 47 | QMutex m_writeLock; 48 | QReadWriteLock m_contentLock; 49 | ASYNC_VALUE_STATE m_state; 50 | 51 | struct Waiter 52 | { 53 | QWaitCondition waitValue; 54 | 55 | quint16 subWaiters = 0; 56 | QWaitCondition waitSubWaiters; 57 | }; 58 | Waiter* m_waiter = nullptr; 59 | }; 60 | 61 | #endif // ASYNC_VALUE_BASE_H 62 | -------------------------------------------------------------------------------- /qt-async-lib/values/AsyncValueRunNetwork.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_VALUE_RUN_NETWORK_H 18 | #define ASYNC_VALUE_RUN_NETWORK_H 19 | 20 | #include 21 | #include 22 | #include "../third_party/scope_exit.h" 23 | 24 | template 25 | bool asyncValueRunNetwork(QNetworkReply* reply, AsyncValueType& value, Func&& func, ProgressArgs&& ...progressArgs) 26 | { 27 | auto progress = std::make_unique(std::forward(progressArgs)...); 28 | auto progressPtr = progress.get(); 29 | 30 | if (!value.startProgress(std::move(progress))) 31 | return false; 32 | 33 | // forward progress 34 | QObject::connect(reply, &QNetworkReply::downloadProgress, [progressPtr](qint64 bytesReceived, qint64 bytesTotal){ 35 | progressPtr->setProgress(bytesReceived, bytesTotal); 36 | }); 37 | 38 | // post processing 39 | QObject::connect(reply, &QNetworkReply::finished, [ reply, 40 | &value, 41 | progressPtr, 42 | func = std::forward(func)](){ 43 | SCOPE_EXIT { 44 | reply->deleteLater(); 45 | // finish progress 46 | value.completeProgress(progressPtr); 47 | }; 48 | 49 | func(*reply, value); 50 | }); 51 | 52 | return true; 53 | } 54 | 55 | template 56 | bool asyncValueRunNetwork(QNetworkAccessManager* networkManager, const QNetworkRequest &request, AsyncValueType& value, Func&& func, ProgressArgs&& ...progressArgs) 57 | { 58 | auto reply = networkManager->get(request); 59 | 60 | if (!reply) 61 | return false; 62 | 63 | return asyncValueRunNetwork(reply, value, std::forward(func), std::forward(progressArgs)...); 64 | } 65 | 66 | #endif // ASYNC_VALUE_RUN_NETWORK_H 67 | -------------------------------------------------------------------------------- /qt-async-lib/values/AsyncValueRunThread.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_VALUE_RUN_THREAD_H 18 | #define ASYNC_VALUE_RUN_THREAD_H 19 | 20 | #include 21 | #include "../third_party/scope_exit.h" 22 | 23 | template 24 | bool asyncValueRunThread(AsyncValueType& value, Func&& func, ProgressArgs&& ...progressArgs) 25 | { 26 | auto progress = std::make_unique(std::forward(progressArgs)...); 27 | auto progressPtr = progress.get(); 28 | 29 | if (!value.startProgress(std::move(progress))) 30 | return false; 31 | 32 | auto thread = QThread::create([&value, progressPtr, func = std::forward(func)]() { 33 | SCOPE_EXIT { 34 | // finish progress 35 | value.completeProgress(progressPtr); 36 | }; 37 | 38 | // run calculation 39 | func(*progressPtr, value); 40 | }); 41 | 42 | if (!thread) 43 | return false; 44 | 45 | // delete thread on complete 46 | QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater); 47 | 48 | thread->start(); 49 | 50 | return true; 51 | } 52 | 53 | #endif // ASYNC_VALUE_RUN_THREAD_H 54 | -------------------------------------------------------------------------------- /qt-async-lib/values/AsyncValueRunThreadPool.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_VALUE_RUN_THREAD_POOL_H 18 | #define ASYNC_VALUE_RUN_THREAD_POOL_H 19 | 20 | #include 21 | #include 22 | #include "../third_party/scope_exit.h" 23 | 24 | template 25 | bool asyncValueRunThreadPool(QThreadPool *pool, AsyncValueType& value, Func&& func, ProgressArgs&& ...progressArgs) 26 | { 27 | auto progress = std::make_unique(std::forward(progressArgs)...); 28 | auto progressPtr = progress.get(); 29 | 30 | if (!value.startProgress(std::move(progress))) 31 | return false; 32 | 33 | QtConcurrent::run(pool, [&value, progressPtr, func = std::forward(func)](){ 34 | SCOPE_EXIT { 35 | // finish progress 36 | value.completeProgress(progressPtr); 37 | }; 38 | 39 | // run calculation 40 | func(*progressPtr, value); 41 | }); 42 | 43 | return true; 44 | } 45 | 46 | template 47 | bool asyncValueRunThreadPool(AsyncValueType& value, Func&& func, ProgressArgs&& ...progressArgs) 48 | { 49 | return asyncValueRunThreadPool(QThreadPool::globalInstance(), value, std::forward(func), std::forward(progressArgs)...); 50 | } 51 | 52 | #endif // ASYNC_VALUE_RUN_THREAD_POOL_H 53 | -------------------------------------------------------------------------------- /qt-async-lib/values/AsyncValueRunable.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_VALUE_RUNABLE_H 18 | #define ASYNC_VALUE_RUNABLE_H 19 | 20 | #include "AsyncValueTemplate.h" 21 | #include "AsyncError.h" 22 | #include "AsyncProgress.h" 23 | #include 24 | 25 | template 26 | class AsyncValueRunableAbstract : public AsyncValueTemplate 27 | { 28 | public: 29 | using ValueType = ValueType_t; 30 | using ErrorType = ErrorType_t; 31 | using ProgressType = ProgressType_t; 32 | using ThisType = AsyncValueRunableAbstract; 33 | using BaseType = AsyncValueTemplate; 34 | using RunFnType = std::function; 35 | 36 | // constructors 37 | using BaseType::BaseType; 38 | 39 | void run() 40 | { 41 | bool isInProgress = BaseType::accessProgress([](ProgressType& progress) { 42 | // if we are in progress already -> just request rerun 43 | progress.requestRerun(); 44 | }); 45 | 46 | if (isInProgress) 47 | return; 48 | 49 | // run later 50 | deferImpl([this] (ProgressType& progress, ThisType&) { 51 | 52 | for (;;) 53 | { 54 | // try to calculate value 55 | runImpl(progress); 56 | // if no rerun was requested -> we good to exit 57 | if (!progress.resetIfRerunRequested()) 58 | break; 59 | } 60 | 61 | }); 62 | } 63 | 64 | protected: 65 | virtual void deferImpl(RunFnType&& func) = 0; 66 | virtual void runImpl(ProgressType& progress) = 0; 67 | }; 68 | 69 | 70 | template 71 | class AsyncValueRunableFn : public AsyncValueTemplate 72 | { 73 | public: 74 | using ValueType = ValueType_t; 75 | using ErrorType = ErrorType_t; 76 | using ProgressType = ProgressType_t; 77 | using ThisType = AsyncValueRunableFn; 78 | using BaseType = AsyncValueTemplate; 79 | using RunFnType = std::function; 80 | using DeferFnType = std::function; 81 | 82 | // constructors 83 | using BaseType::BaseType; 84 | 85 | DeferFnType deferFn; 86 | RunFnType runFn; 87 | 88 | void run() 89 | { 90 | bool isInProgress = BaseType::accessProgress([](ProgressType& progress) { 91 | // if we are in progress already -> just request rerun 92 | progress.requestRerun(); 93 | }); 94 | 95 | if (isInProgress) 96 | return; 97 | 98 | // run later 99 | deferFn([this] (ProgressType& progress, ThisType& value) { 100 | 101 | for (;;) 102 | { 103 | // try to calculate value 104 | runFn(progress, value); 105 | // if no rerun was requested -> we good to exit 106 | if (!progress.resetIfRerunRequested()) 107 | break; 108 | } 109 | 110 | }); 111 | } 112 | }; 113 | 114 | #endif // ASYNC_VALUE_RUNABLE_H 115 | -------------------------------------------------------------------------------- /qt-async-lib/values/AsyncValueTemplate.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_VALUE_TEMPLATE_H 18 | #define ASYNC_VALUE_TEMPLATE_H 19 | 20 | #include 21 | #include "AsyncValueBase.h" 22 | #include "AsyncTrackErrorsPolicy.h" 23 | 24 | struct AsyncNoOp 25 | { 26 | template 27 | void operator()(const T&) const {} 28 | }; 29 | 30 | struct AsyncInitByValue {}; 31 | struct AsyncInitByError {}; 32 | 33 | 34 | template 35 | class AsyncValueTemplate : public AsyncValueBase 36 | { 37 | public: 38 | using ValueType = ValueType_t; 39 | using ErrorType = ErrorType_t; 40 | using ProgressType = ProgressType_t; 41 | 42 | template 43 | explicit AsyncValueTemplate(QObject* parent, AsyncInitByValue, Args&& ...arguments) 44 | : AsyncValueBase(ASYNC_VALUE_STATE::VALUE, parent) 45 | { 46 | emplaceValue(std::forward(arguments)...); 47 | } 48 | 49 | template 50 | explicit AsyncValueTemplate(AsyncInitByValue, Args&& ...arguments) 51 | : AsyncValueBase(ASYNC_VALUE_STATE::VALUE, nullptr) 52 | { 53 | emplaceValue(std::forward(arguments)...); 54 | } 55 | 56 | ~AsyncValueTemplate() 57 | { 58 | if (m_state == ASYNC_VALUE_STATE::PROGRESS) 59 | m_trackErrors.inProgressWhileDestruct(); 60 | } 61 | 62 | template 63 | void emplaceValue(Args&& ...arguments) 64 | { 65 | moveValue(std::make_unique(std::forward(arguments)...)); 66 | } 67 | 68 | void moveValue(std::unique_ptr value) 69 | { 70 | m_trackErrors.trackEmitDeadlock(); 71 | 72 | Content oldContent; 73 | 74 | QMutexLocker writeLocker(&m_writeLock); 75 | { 76 | QWriteLocker locker(&m_contentLock); 77 | 78 | oldContent = std::move(m_content); 79 | m_content.value = std::move(value); 80 | 81 | // don't change state until stopProgress happen 82 | if (m_state == ASYNC_VALUE_STATE::PROGRESS) 83 | return; 84 | 85 | m_state = ASYNC_VALUE_STATE::VALUE; 86 | } 87 | 88 | emitStateChanged(); 89 | 90 | // notify all waiters 91 | if (m_waiter) 92 | m_waiter->waitValue.wakeAll(); 93 | } 94 | 95 | template 96 | explicit AsyncValueTemplate(QObject* parent, AsyncInitByError, Args&& ...arguments) 97 | : AsyncValueBase(ASYNC_VALUE_STATE::ERROR, parent) 98 | { 99 | emplaceError(std::forward(arguments)...); 100 | } 101 | 102 | template 103 | explicit AsyncValueTemplate(AsyncInitByError, Args&& ...arguments) 104 | : AsyncValueBase(ASYNC_VALUE_STATE::ERROR, nullptr) 105 | { 106 | emplaceError(std::forward(arguments)...); 107 | } 108 | 109 | template 110 | void emplaceError(Args&& ...arguments) 111 | { 112 | moveError(std::make_unique(std::forward(arguments)...)); 113 | } 114 | 115 | void moveError(std::unique_ptr error) 116 | { 117 | m_trackErrors.trackEmitDeadlock(); 118 | 119 | Content oldContent; 120 | 121 | QMutexLocker writeLocker(&m_writeLock); 122 | { 123 | QWriteLocker locker(&m_contentLock); 124 | 125 | oldContent = std::move(m_content); 126 | m_content.error = std::move(error); 127 | 128 | // don't change state until stopProgress happen 129 | if (m_state == ASYNC_VALUE_STATE::PROGRESS) 130 | return; 131 | 132 | m_state = ASYNC_VALUE_STATE::ERROR; 133 | } 134 | 135 | emitStateChanged(); 136 | 137 | // notify all waiters 138 | if (m_waiter) 139 | m_waiter->waitValue.wakeAll(); 140 | } 141 | 142 | bool startProgress(std::unique_ptr progress) 143 | { 144 | Q_ASSERT(progress); 145 | 146 | m_trackErrors.trackEmitDeadlock(); 147 | 148 | Content oldContent; 149 | 150 | QMutexLocker writeLocker(&m_writeLock); 151 | 152 | if (m_state == ASYNC_VALUE_STATE::PROGRESS) 153 | { 154 | m_trackErrors.startProgressWhileInProgress(); 155 | return false; 156 | } 157 | 158 | { 159 | QWriteLocker locker(&m_contentLock); 160 | 161 | oldContent = std::move(m_content); 162 | m_progress = std::move(progress); 163 | m_state = ASYNC_VALUE_STATE::PROGRESS; 164 | 165 | #ifdef QT_DEBUG 166 | Q_ASSERT(!m_progress->isInUse() && "Progress is used already"); 167 | m_progress->setInUse(true); 168 | #endif 169 | } 170 | 171 | emitStateChanged(); 172 | 173 | return true; 174 | } 175 | 176 | bool completeProgress(ProgressType* progress) 177 | { 178 | #ifdef QT_DEBUG 179 | Q_ASSERT(progress); 180 | Q_ASSERT(progress->isInUse() && "Progress should be used"); 181 | progress->setInUse(false); 182 | #endif 183 | 184 | QMutexLocker writeLocker(&m_writeLock); 185 | 186 | if (progress != m_progress.get()) 187 | { 188 | m_trackErrors.tryCompleteAlienProgress(); 189 | return false; 190 | } 191 | 192 | { 193 | QWriteLocker locker(&m_contentLock); 194 | 195 | if (m_content.value) 196 | m_state = ASYNC_VALUE_STATE::VALUE; 197 | else if (m_content.error) 198 | m_state = ASYNC_VALUE_STATE::ERROR; 199 | else 200 | { 201 | m_trackErrors.incompleteProgress(); 202 | return false; 203 | } 204 | 205 | m_progress = nullptr; 206 | } 207 | 208 | emitStateChanged(); 209 | 210 | // notify all waiters 211 | if (m_waiter) 212 | m_waiter->waitValue.wakeAll(); 213 | 214 | return true; 215 | } 216 | 217 | template 218 | void access(ValuePred valuePred, ErrorPred errorPred, ProgressPred progressPred) 219 | { 220 | QReadLocker locker(&m_contentLock); 221 | 222 | switch (m_state) 223 | { 224 | case ASYNC_VALUE_STATE::VALUE: 225 | valuePred(*m_content.value); 226 | break; 227 | 228 | case ASYNC_VALUE_STATE::ERROR: 229 | errorPred(*m_content.error); 230 | break; 231 | 232 | case ASYNC_VALUE_STATE::PROGRESS: 233 | progressPred(*m_progress); 234 | break; 235 | } 236 | } 237 | 238 | template 239 | bool access(ValuePred valuePred, ErrorPred errorPred) 240 | { 241 | QReadLocker locker(&m_contentLock); 242 | 243 | switch (m_state) 244 | { 245 | case ASYNC_VALUE_STATE::VALUE: 246 | valuePred(*m_content.value); 247 | return true; 248 | 249 | case ASYNC_VALUE_STATE::ERROR: 250 | errorPred(*m_content.error); 251 | return true; 252 | 253 | default: 254 | return false; 255 | } 256 | } 257 | 258 | template 259 | bool access(Pred valuePred) 260 | { 261 | QReadLocker locker(&m_contentLock); 262 | 263 | if (m_state != ASYNC_VALUE_STATE::VALUE) 264 | return false; 265 | 266 | valuePred(*m_content.value); 267 | return true; 268 | } 269 | 270 | template 271 | bool accessValue(Pred valuePred) 272 | { 273 | return access(valuePred); 274 | } 275 | 276 | template 277 | bool accessError(Pred errorPred) 278 | { 279 | QReadLocker locker(&m_contentLock); 280 | 281 | if (m_state != ASYNC_VALUE_STATE::ERROR) 282 | return false; 283 | 284 | errorPred(*m_content.error); 285 | return true; 286 | } 287 | 288 | template 289 | bool accessProgress(Pred progressPred) 290 | { 291 | QReadLocker locker(&m_contentLock); 292 | 293 | if (m_state != ASYNC_VALUE_STATE::PROGRESS) 294 | return false; 295 | 296 | progressPred(*m_progress); 297 | return true; 298 | } 299 | 300 | template 301 | void wait(ValuePred valuePred, ErrorPred errorPred) 302 | { 303 | // easy case we have value or error 304 | if (access(valuePred, errorPred)) 305 | return; 306 | 307 | // lock async value 308 | QMutexLocker writeLocker(&m_writeLock); 309 | // check easy case again 310 | if (access(valuePred, errorPred)) 311 | return; 312 | 313 | // if we are the first waiters 314 | // create Waiter on stack and assign it to m_waiter 315 | // all subsequent waiters will use this one 316 | if (!m_waiter) 317 | { 318 | Waiter theWaiter; 319 | 320 | SCOPE_EXIT { 321 | if (m_waiter->subWaiters > 0) 322 | { 323 | do 324 | { 325 | // repeatedly wait for all sub waiters 326 | // because of spurious wakeups 327 | m_waiter->waitSubWaiters.wait(&m_writeLock); 328 | } while (m_waiter->subWaiters != 0); 329 | } 330 | 331 | // unregister self as main waiter 332 | m_waiter = nullptr; 333 | }; 334 | 335 | // register self as main waiter 336 | m_waiter = &theWaiter; 337 | 338 | do 339 | { 340 | // repeatedly wait for value or error 341 | // because of spurious wakeups 342 | m_waiter->waitValue.wait(&m_writeLock); 343 | } while (!access(valuePred, errorPred)); 344 | } 345 | else 346 | { 347 | SCOPE_EXIT { 348 | // unregister self as subwaiter 349 | m_waiter->subWaiters -= 1; 350 | // if no subwaiters -> notify main waiter to release 351 | if (m_waiter->subWaiters == 0) 352 | m_waiter->waitSubWaiters.wakeAll(); 353 | }; 354 | 355 | // register self as subwaiter 356 | m_waiter->subWaiters += 1; 357 | 358 | do 359 | { 360 | // repeatedly wait for value or error 361 | // because of spurious wakeups 362 | m_waiter->waitValue.wait(&m_writeLock); 363 | } while (!access(valuePred, errorPred)); 364 | } 365 | } 366 | 367 | void wait() 368 | { 369 | wait(AsyncNoOp(), AsyncNoOp()); 370 | } 371 | 372 | void stopAndWait() 373 | { 374 | accessProgress([](ProgressType& progress){ 375 | progress.requestStop(); 376 | }); 377 | wait(); 378 | } 379 | 380 | private: 381 | void emitStateChanged() 382 | { 383 | using EmitGuardType = typename TrackErrorsPolicy_t::EmitGuardType; 384 | EmitGuardType emitGuard(m_trackErrors); 385 | 386 | emit stateChanged(m_state); 387 | } 388 | 389 | struct Content 390 | { 391 | std::unique_ptr value; 392 | std::unique_ptr error; 393 | }; 394 | Content m_content; 395 | 396 | std::unique_ptr m_progress; 397 | 398 | TrackErrorsPolicy_t m_trackErrors; 399 | }; 400 | 401 | #endif // ASYNC_VALUE_TEMPLATE_H 402 | -------------------------------------------------------------------------------- /qt-async-lib/widgets/AsyncWidget.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_WIDGET_H 18 | #define ASYNC_WIDGET_H 19 | 20 | #include 21 | #include "values/AsyncValue.h" 22 | #include "AsyncWidgetBase.h" 23 | #include "AsyncWidgetError.h" 24 | #include "AsyncWidgetProgressBar.h" 25 | 26 | template 27 | class AsyncWidget : public AsyncWidgetBase 28 | { 29 | public: 30 | using ValueType = typename AsyncValueType::ValueType; 31 | using ErrorType = typename AsyncValueType::ErrorType; 32 | using ProgressType = typename AsyncValueType::ProgressType; 33 | 34 | using AsyncWidgetBase::AsyncWidgetBase; 35 | 36 | protected: 37 | QWidget* createValueWidgetImpl(ValueType& /*value*/, QWidget* parent) override 38 | { 39 | return this->createLabel("", parent); 40 | } 41 | 42 | QWidget* createErrorWidgetImpl(ErrorType& error, QWidget* parent) override 43 | { 44 | return new AsyncWidgetError(error, parent); 45 | } 46 | 47 | QWidget* createProgressWidgetImpl(ProgressType& progress, QWidget* parent) override 48 | { 49 | return new AsyncWidgetProgressBar(progress, parent); 50 | } 51 | }; 52 | 53 | template 54 | class AsyncWidgetFn : public AsyncWidget 55 | { 56 | public: 57 | using ValueType = typename AsyncValueType::ValueType; 58 | using ErrorType = typename AsyncValueType::ErrorType; 59 | using ProgressType = typename AsyncValueType::ProgressType; 60 | 61 | using AsyncWidget::AsyncWidget; 62 | 63 | std::function createValueWidget; 64 | std::function createErrorWidget; 65 | std::function createProgressWidget; 66 | 67 | protected: 68 | QWidget* createValueWidgetImpl(ValueType& value, QWidget* parent) override 69 | { 70 | if (createValueWidget) 71 | return createValueWidget(value, parent); 72 | else 73 | return AsyncWidget::createValueWidgetImpl(value, parent); 74 | } 75 | 76 | QWidget* createErrorWidgetImpl(ErrorType& error, QWidget* parent) override 77 | { 78 | if (createErrorWidget) 79 | return createErrorWidget(error, parent); 80 | else 81 | return AsyncWidget::createErrorWidgetImpl(error, parent); 82 | } 83 | 84 | QWidget* createProgressWidgetImpl(ProgressType& progress, QWidget* parent) override 85 | { 86 | if (createProgressWidget) 87 | return createProgressWidget(progress, parent); 88 | else 89 | return AsyncWidget::createProgressWidgetImpl(progress, parent); 90 | } 91 | }; 92 | 93 | #endif // ASYNC_WIDGET_H 94 | -------------------------------------------------------------------------------- /qt-async-lib/widgets/AsyncWidgetBase.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_WIDGET_BASE_H 18 | #define ASYNC_WIDGET_BASE_H 19 | 20 | #include "AsyncWidgetProxy.h" 21 | #include "values/AsyncValueBase.h" 22 | 23 | template 24 | class AsyncWidgetBase : public AsyncWidgetProxy 25 | { 26 | public: 27 | using AsyncWidgetProxy::AsyncWidgetProxy; 28 | 29 | void setValue(AsyncValueType* asyncValue) 30 | { 31 | if (m_asyncValue == asyncValue) 32 | return; 33 | 34 | if (m_asyncValue) 35 | QObject::disconnect(m_asyncValue, &AsyncValueBase::stateChanged, this, &AsyncWidgetBase::onValueStateChanged); 36 | setContentWidget(nullptr); 37 | 38 | m_asyncValue = asyncValue; 39 | if (m_asyncValue) 40 | QObject::connect(m_asyncValue, &AsyncValueBase::stateChanged, this, &AsyncWidgetBase::onValueStateChanged); 41 | updateContent(); 42 | } 43 | 44 | protected: 45 | using ValueType = typename AsyncValueType::ValueType; 46 | using ErrorType = typename AsyncValueType::ErrorType; 47 | using ProgressType = typename AsyncValueType::ProgressType; 48 | 49 | virtual QWidget* createValueWidgetImpl(ValueType& value, QWidget* parent) = 0; 50 | virtual QWidget* createErrorWidgetImpl(ErrorType& error, QWidget* parent) = 0; 51 | virtual QWidget* createProgressWidgetImpl(ProgressType& progress, QWidget* parent) = 0; 52 | virtual QWidget* createNoAsyncValueWidgetImpl(QWidget* parent) { return createLabel("", parent); } 53 | 54 | private: 55 | void onValueStateChanged(ASYNC_VALUE_STATE /*state*/) 56 | { 57 | updateContent(); 58 | } 59 | 60 | void updateContent() 61 | { 62 | if (!m_asyncValue) 63 | { 64 | setContentWidget(createNoAsyncValueWidgetImpl(this)); 65 | return; 66 | } 67 | 68 | QWidget* newWidget = nullptr; 69 | 70 | m_asyncValue->access([&newWidget, this](ValueType& value){ 71 | newWidget = createValueWidgetImpl(value, this); 72 | }, [&newWidget, this](ErrorType& error){ 73 | newWidget = createErrorWidgetImpl(error, this); 74 | }, [&newWidget, this](ProgressType& progress){ 75 | newWidget = createProgressWidgetImpl(progress, this); 76 | }); 77 | 78 | Q_ASSERT(newWidget); 79 | if (!newWidget) 80 | newWidget = createLabel("", this); 81 | 82 | setContentWidget(newWidget); 83 | } 84 | 85 | AsyncValueType* m_asyncValue = nullptr; 86 | }; 87 | 88 | #endif // ASYNC_WIDGET_BASE_H 89 | -------------------------------------------------------------------------------- /qt-async-lib/widgets/AsyncWidgetError.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "AsyncWidgetError.h" 18 | 19 | AsyncWidgetError::AsyncWidgetError(const AsyncError& error, QWidget* parent) 20 | : QLabel(parent), 21 | m_error(error) 22 | { 23 | setWordWrap(true); 24 | setAlignment(Qt::AlignCenter); 25 | setText(m_error.text()); 26 | } 27 | -------------------------------------------------------------------------------- /qt-async-lib/widgets/AsyncWidgetError.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_WIDGET_ERROR_H 18 | #define ASYNC_WIDGET_ERROR_H 19 | 20 | #include 21 | #include "values/AsyncError.h" 22 | 23 | class AsyncWidgetError : public QLabel 24 | { 25 | Q_OBJECT 26 | Q_DISABLE_COPY(AsyncWidgetError) 27 | 28 | public: 29 | explicit AsyncWidgetError(const AsyncError& error, QWidget* parent); 30 | 31 | private: 32 | const AsyncError& m_error; 33 | }; 34 | 35 | #endif // ASYNC_WIDGET_ERROR_H 36 | -------------------------------------------------------------------------------- /qt-async-lib/widgets/AsyncWidgetProgressBar.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "AsyncWidgetProgressBar.h" 18 | #include "../Config.h" 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | AsyncWidgetProgressBar::AsyncWidgetProgressBar(AsyncProgress& progress, QWidget* parent) 28 | : QFrame(parent), 29 | m_progress(progress) 30 | { 31 | auto layout = new QVBoxLayout(this); 32 | layout->setSpacing(6); 33 | layout->setContentsMargins(11, 11, 11, 11); 34 | 35 | // top spacer 36 | { 37 | auto spacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding); 38 | layout->addItem(spacer); 39 | } 40 | 41 | // content 42 | { 43 | // message 44 | { 45 | m_message = new QLabel(this); 46 | layout->addWidget(m_message); 47 | } 48 | 49 | { 50 | auto subLayout = new QHBoxLayout(); 51 | layout->addLayout(subLayout); 52 | 53 | subLayout->setSpacing(6); 54 | 55 | // progress 56 | { 57 | m_progressBar = new QProgressBar(this); 58 | m_progressBar->setTextVisible(false); 59 | m_progressBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); 60 | subLayout->addWidget(m_progressBar); 61 | 62 | m_progressBarTimeLine = new QTimeLine(300, this); 63 | QObject::connect(m_progressBarTimeLine, &QTimeLine::frameChanged, m_progressBar, &QProgressBar::setValue); 64 | } 65 | 66 | // stop button 67 | { 68 | m_stop = new QPushButton(this); 69 | m_stop->setText("Stop"); 70 | subLayout->addWidget(m_stop); 71 | QObject::connect(m_stop, &QPushButton::clicked, this, &AsyncWidgetProgressBar::onStopClicked); 72 | } 73 | } 74 | } 75 | 76 | // bottom spacer 77 | { 78 | auto spacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding); 79 | layout->addItem(spacer); 80 | } 81 | 82 | auto timer = new QTimer(this); 83 | connect(timer, &QTimer::timeout, this, &AsyncWidgetProgressBar::updateContent); 84 | timer->start(ASYNC_PROGRESS_WIDGET_UPDATE_TIMEOUT); 85 | 86 | updateContent(); 87 | } 88 | 89 | void AsyncWidgetProgressBar::onStopClicked(bool /*checked*/) 90 | { 91 | m_progress.requestStop(); 92 | } 93 | 94 | void AsyncWidgetProgressBar::updateContent() 95 | { 96 | if (m_progress.isStopRequested()) 97 | m_message->setText(m_progress.message()+"(Stopping...)"); 98 | else 99 | m_message->setText(m_progress.message()); 100 | 101 | m_stop->setVisible(m_progress.canRequestStop()); 102 | 103 | m_progressBarTimeLine->stop(); 104 | m_progressBarTimeLine->setFrameRange(m_progressBar->value(), int(m_progress.progress() * 100.f)); 105 | m_progressBarTimeLine->start(); 106 | } 107 | -------------------------------------------------------------------------------- /qt-async-lib/widgets/AsyncWidgetProgressBar.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_WIDGET_PROGRESS_BAR_H 18 | #define ASYNC_WIDGET_PROGRESS_BAR_H 19 | 20 | #include 21 | #include "values/AsyncProgress.h" 22 | 23 | class QLabel; 24 | class QProgressBar; 25 | class QPushButton; 26 | class QTimeLine; 27 | 28 | class AsyncWidgetProgressBar : public QFrame 29 | { 30 | Q_OBJECT 31 | Q_DISABLE_COPY(AsyncWidgetProgressBar) 32 | 33 | public: 34 | explicit AsyncWidgetProgressBar(AsyncProgress& progress, QWidget* parent); 35 | 36 | private slots: 37 | void onStopClicked(bool checked); 38 | 39 | private: 40 | void updateContent(); 41 | 42 | AsyncProgress& m_progress; 43 | 44 | QLabel* m_message = nullptr; 45 | QProgressBar* m_progressBar = nullptr; 46 | QPushButton* m_stop = nullptr; 47 | QTimeLine* m_progressBarTimeLine = nullptr; 48 | }; 49 | 50 | #endif // ASYNC_WIDGET_PROGRESS_BAR_H 51 | -------------------------------------------------------------------------------- /qt-async-lib/widgets/AsyncWidgetProgressCircle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "AsyncWidgetProgressCircle.h" 18 | #include "../Config.h" 19 | #include "../third_party/QtProgressCircle/ProgressCircle.h" 20 | #include 21 | 22 | AsyncWidgetProgressCircle::AsyncWidgetProgressCircle(AsyncProgress& progress, QWidget* parent) 23 | : QFrame(parent), 24 | m_progress(progress) 25 | { 26 | m_progressCircle = new ProgressCircle(this); 27 | m_progressCircle->setMaximum(100); 28 | m_progressCircle->setValue(0); 29 | 30 | auto timer = new QTimer(this); 31 | connect(timer, &QTimer::timeout, this, &AsyncWidgetProgressCircle::updateContent); 32 | timer->start(ASYNC_PROGRESS_WIDGET_UPDATE_TIMEOUT); 33 | 34 | updateContent(); 35 | } 36 | 37 | AsyncWidgetProgressCircle::~AsyncWidgetProgressCircle() 38 | { 39 | } 40 | 41 | void AsyncWidgetProgressCircle::resizeEvent(QResizeEvent *event) 42 | { 43 | QFrame::resizeEvent(event); 44 | 45 | // center progress circle widget 46 | auto circleRect = rect(); 47 | circleRect.setTop(circleRect.top() + circleRect.height() / 3); 48 | circleRect.setBottom(circleRect.bottom() - circleRect.height() / 3); 49 | circleRect.setLeft(circleRect.left() + circleRect.width() / 3); 50 | circleRect.setRight(circleRect.right() - circleRect.width() / 3); 51 | m_progressCircle->setGeometry(circleRect); 52 | } 53 | 54 | void AsyncWidgetProgressCircle::updateContent() 55 | { 56 | m_progressCircle->setValue(static_cast(m_progress.progress()*100.f)); 57 | if (m_progress.isStopRequested()) 58 | m_progressCircle->setColor(QColor(255, 128, 64)); 59 | } 60 | -------------------------------------------------------------------------------- /qt-async-lib/widgets/AsyncWidgetProgressCircle.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_WIDGET_PROGRESS_CIRCLE_H 18 | #define ASYNC_WIDGET_PROGRESS_CIRCLE_H 19 | 20 | #include 21 | #include "values/AsyncProgress.h" 22 | 23 | class ProgressCircle; 24 | class QResizeEvent; 25 | 26 | class AsyncWidgetProgressCircle : public QFrame 27 | { 28 | Q_OBJECT 29 | Q_DISABLE_COPY(AsyncWidgetProgressCircle) 30 | 31 | public: 32 | explicit AsyncWidgetProgressCircle(AsyncProgress& progress, QWidget* parent); 33 | ~AsyncWidgetProgressCircle(); 34 | 35 | protected: 36 | void resizeEvent(QResizeEvent *event) override; 37 | 38 | private: 39 | void updateContent(); 40 | 41 | AsyncProgress& m_progress; 42 | 43 | ProgressCircle* m_progressCircle = nullptr; 44 | }; 45 | 46 | #endif // ASYNC_WIDGET_PROGRESS_CIRCLE_H 47 | -------------------------------------------------------------------------------- /qt-async-lib/widgets/AsyncWidgetProgressSpinner.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "AsyncWidgetProgressSpinner.h" 18 | #include "../Config.h" 19 | #include "../third_party/QtWaitingSpinner/waitingspinnerwidget.h" 20 | #include 21 | 22 | AsyncWidgetProgressSpinner::AsyncWidgetProgressSpinner(AsyncProgress& progress, QWidget* parent) 23 | : QFrame(parent), 24 | m_progress(progress) 25 | { 26 | m_spinner = new WaitingSpinnerWidget(this, true, false); 27 | m_spinner->start(); 28 | 29 | auto timer = new QTimer(this); 30 | connect(timer, &QTimer::timeout, this, &AsyncWidgetProgressSpinner::updateContent); 31 | timer->start(ASYNC_PROGRESS_WIDGET_UPDATE_TIMEOUT); 32 | 33 | updateContent(); 34 | } 35 | 36 | AsyncWidgetProgressSpinner::~AsyncWidgetProgressSpinner() 37 | { 38 | } 39 | 40 | void AsyncWidgetProgressSpinner::updateContent() 41 | { 42 | m_spinner->setText(m_progress.message()); 43 | } 44 | -------------------------------------------------------------------------------- /qt-async-lib/widgets/AsyncWidgetProgressSpinner.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_WIDGET_PROGRESS_SPINNER_H 18 | #define ASYNC_WIDGET_PROGRESS_SPINNER_H 19 | 20 | #include 21 | #include "values/AsyncProgress.h" 22 | 23 | class WaitingSpinnerWidget; 24 | 25 | class AsyncWidgetProgressSpinner : public QFrame 26 | { 27 | Q_OBJECT 28 | Q_DISABLE_COPY(AsyncWidgetProgressSpinner) 29 | 30 | public: 31 | explicit AsyncWidgetProgressSpinner(AsyncProgress& progress, QWidget* parent); 32 | ~AsyncWidgetProgressSpinner(); 33 | 34 | private: 35 | void updateContent(); 36 | 37 | AsyncProgress& m_progress; 38 | 39 | WaitingSpinnerWidget* m_spinner = nullptr; 40 | }; 41 | 42 | #endif // ASYNC_WIDGET_PROGRESS_SPINNER_H 43 | -------------------------------------------------------------------------------- /qt-async-lib/widgets/AsyncWidgetProxy.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "AsyncWidgetProxy.h" 18 | #include 19 | #include 20 | 21 | void AsyncWidgetProxy::setContentWidget(QWidget* content) 22 | { 23 | if (m_content == content) 24 | return; 25 | 26 | auto oldContent = m_content; 27 | if (oldContent) 28 | oldContent->hide(); 29 | 30 | m_content = content; 31 | 32 | if (m_content) 33 | { 34 | m_content->setParent(this); 35 | m_content->setGeometry(rect()); 36 | m_content->show(); 37 | } 38 | 39 | if (oldContent) 40 | delete oldContent; 41 | } 42 | 43 | void AsyncWidgetProxy::resizeEvent(QResizeEvent *event) 44 | { 45 | QWidget::resizeEvent(event); 46 | 47 | if (m_content) 48 | m_content->setGeometry(QRect(QPoint(0, 0), event->size())); 49 | } 50 | 51 | QWidget* AsyncWidgetProxy::createLabel(QString text, QWidget* parent) 52 | { 53 | auto label = new QLabel(text, parent); 54 | label->setObjectName("AsyncLabel"); 55 | label->setWordWrap(true); 56 | label->setAlignment(Qt::AlignCenter); 57 | return label; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /qt-async-lib/widgets/AsyncWidgetProxy.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018 Alex Zhondin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef ASYNC_WIDGET_PROXY_H 18 | #define ASYNC_WIDGET_PROXY_H 19 | 20 | #include 21 | 22 | class AsyncWidgetProxy : public QWidget 23 | { 24 | Q_OBJECT 25 | Q_DISABLE_COPY(AsyncWidgetProxy) 26 | 27 | public: 28 | using QWidget::QWidget; 29 | 30 | QWidget* contentWidget() const { return m_content; } 31 | void setContentWidget(QWidget* content); 32 | 33 | static QWidget* createLabel(QString text, QWidget* parent); 34 | 35 | protected: 36 | void resizeEvent(QResizeEvent *event) override; 37 | 38 | private: 39 | QWidget* m_content = nullptr; 40 | }; 41 | 42 | #endif // ASYNC_WIDGET_PROXY_H 43 | -------------------------------------------------------------------------------- /qt-async.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = subdirs 2 | SUBDIRS += qt-async-lib\ 3 | tests\ 4 | demo 5 | 6 | tests.depends = qt-async-lib 7 | demo.depends = qt-async-lib 8 | -------------------------------------------------------------------------------- /tests/TestAsyncValue.cpp: -------------------------------------------------------------------------------- 1 | #include "TestAsyncValue.h" 2 | #include 3 | #include "values/AsyncValue.h" 4 | #include "values/AsyncValueRunThread.h" 5 | #include "values/AsyncValueRunThreadPool.h" 6 | #include "values/AsyncValueRunNetwork.h" 7 | #include "values/AsyncValueRunable.h" 8 | 9 | void TestAsyncValue::simple() 10 | { 11 | // init by value 12 | { 13 | AsyncValue value(AsyncInitByValue(), 8); 14 | 15 | { 16 | auto res = value.accessValue([](const int& value){ 17 | QCOMPARE(value, 8); 18 | }); 19 | QVERIFY(res); 20 | 21 | res = value.accessError(AsyncNoOp()); 22 | QVERIFY(!res); 23 | 24 | res = value.accessProgress(AsyncNoOp()); 25 | QVERIFY(!res); 26 | } 27 | } 28 | 29 | // init by error 30 | { 31 | AsyncValue value(AsyncInitByError(), "no value"); 32 | 33 | { 34 | auto res = value.accessError([](const AsyncError& error){ 35 | QCOMPARE(error.text(), QString("no value")); 36 | }); 37 | QVERIFY(res); 38 | 39 | res = value.accessValue(AsyncNoOp()); 40 | QVERIFY(!res); 41 | 42 | res = value.accessProgress(AsyncNoOp()); 43 | QVERIFY(!res); 44 | } 45 | } 46 | } 47 | 48 | void TestAsyncValue::runInThread() 49 | { 50 | AsyncValue value(AsyncInitByValue(), 8); 51 | 52 | asyncValueRunThread(value, [&](AsyncProgress&, AsyncValue& value) { 53 | QThread::sleep(1); 54 | value.emplaceValue(42); 55 | }, "", ASYNC_CAN_REQUEST_STOP::NO); 56 | 57 | value.wait([](int val){ 58 | QCOMPARE(val, 42); 59 | }, AsyncNoOp()); 60 | } 61 | 62 | void TestAsyncValue::runInThreadPool() 63 | { 64 | AsyncValue value(AsyncInitByValue(), 8); 65 | 66 | asyncValueRunThreadPool(value, [&](AsyncProgress&, AsyncValue& value) { 67 | QThread::sleep(1); 68 | value.emplaceValue(42); 69 | }, "", ASYNC_CAN_REQUEST_STOP::NO); 70 | 71 | value.wait([](int val){ 72 | QCOMPARE(val, 42); 73 | }, AsyncNoOp()); 74 | } 75 | 76 | void TestAsyncValue::catchDeadlock() 77 | { 78 | AsyncValue value(AsyncInitByValue(), 8); 79 | 80 | QObject::connect(&value, &AsyncValue::stateChanged, [&value](ASYNC_VALUE_STATE){ 81 | value.emplaceValue(3); 82 | }); 83 | 84 | bool deadlock = false; 85 | 86 | try 87 | { 88 | value.emplaceValue(7); 89 | } 90 | catch (const std::logic_error&) 91 | { 92 | deadlock = true; 93 | } 94 | 95 | QVERIFY(deadlock); 96 | } 97 | 98 | void TestAsyncValue::wait() 99 | { 100 | AsyncValue value(AsyncInitByValue(), 8); 101 | 102 | QMutex lock; 103 | QWaitCondition isRunning; 104 | QSemaphore sem(15); 105 | sem.acquire(15); 106 | 107 | QThreadPool pool; 108 | pool.setMaxThreadCount(20); 109 | 110 | std::vector> clients; 111 | for (int i = 0; i < 15; ++i) 112 | { 113 | QFuture future = QtConcurrent::run(&pool, [&](){ 114 | { 115 | QMutexLocker locker(&lock); 116 | sem.release(1); 117 | isRunning.wait(&lock); 118 | } 119 | 120 | int res = 0; 121 | 122 | value.wait([&](int val){ 123 | res = val; 124 | }, AsyncNoOp()); 125 | 126 | return res; 127 | }); 128 | 129 | clients.push_back(future); 130 | } 131 | 132 | asyncValueRunThreadPool(&pool, value, [&](AsyncProgress&, AsyncValue& value){ 133 | sem.acquire(15); 134 | isRunning.notify_all(); 135 | QThread::sleep(1); 136 | value.emplaceValue(42); 137 | }, "", ASYNC_CAN_REQUEST_STOP::NO); 138 | 139 | for (auto f : clients) 140 | { 141 | QCOMPARE(f.result(), 42); 142 | } 143 | } 144 | 145 | void TestAsyncValue::run() 146 | { 147 | AsyncValueRunableFn value(AsyncInitByValue(), 8); 148 | 149 | int rerunTotal = 0; 150 | 151 | value.deferFn = [&value](const AsyncValueRunableFn::RunFnType& fn) { 152 | asyncValueRunThreadPool(value, fn, "", ASYNC_CAN_REQUEST_STOP::YES); 153 | }; 154 | value.runFn = [&](AsyncProgressRerun&, AsyncValueRunableFn& value) { 155 | QThread::sleep(1); 156 | value.emplaceValue(42); 157 | ++rerunTotal; 158 | }; 159 | 160 | value.run(); 161 | value.run(); 162 | value.run(); 163 | value.run(); 164 | value.run(); 165 | 166 | value.wait(); 167 | 168 | // at least two runs should happen 169 | QVERIFY(rerunTotal >= 2); 170 | } 171 | 172 | void TestAsyncValue::network() 173 | { 174 | AsyncValue value(AsyncInitByValue(), 8); 175 | QNetworkAccessManager network; 176 | QEventLoop loop; 177 | 178 | asyncValueRunNetwork(&network, QNetworkRequest(QUrl("http://doc.qt.io/qt-5/images/qml-uses-visual-transforms.png")), value, [&loop](const QNetworkReply& reply, AsyncValue& value){ 179 | 180 | // process error 181 | if (reply.error() != QNetworkReply::NoError) 182 | value.emplaceError(QString("Network error code: %1").arg(reply.error())); 183 | else 184 | value.emplaceValue(reply.bytesAvailable()); 185 | 186 | loop.quit(); 187 | }, "", ASYNC_CAN_REQUEST_STOP::NO); 188 | 189 | loop.exec(); 190 | 191 | auto success = value.accessValue([](int value){ 192 | QVERIFY(value > 0); 193 | }); 194 | 195 | QVERIFY(success); 196 | } 197 | -------------------------------------------------------------------------------- /tests/TestAsyncValue.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_ASYNC_VALUE_H 2 | #define TEST_ASYNC_VALUE_H 3 | 4 | #include 5 | 6 | class TestAsyncValue: public QObject 7 | { 8 | Q_OBJECT 9 | 10 | public: 11 | Q_INVOKABLE TestAsyncValue() {} 12 | 13 | private Q_SLOTS: 14 | 15 | void simple(); 16 | void runInThread(); 17 | void runInThreadPool(); 18 | void catchDeadlock(); 19 | void wait(); 20 | void run(); 21 | void network(); 22 | }; 23 | 24 | #endif // TEST_ASYNC_VALUE_H 25 | -------------------------------------------------------------------------------- /tests/main.cpp: -------------------------------------------------------------------------------- 1 | #include "TestAsyncValue.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QCoreApplication app(argc, argv); 7 | 8 | int result = 0; 9 | 10 | QList tests; 11 | 12 | // register tests 13 | tests.append(&TestAsyncValue::staticMetaObject); 14 | 15 | // run tests 16 | foreach (const QMetaObject *testMetaObject, tests) 17 | { 18 | QScopedPointer test(testMetaObject->newInstance()); 19 | Q_ASSERT(test); 20 | 21 | if (test) 22 | { 23 | result |= QTest::qExec(test.data(), argc, argv); 24 | } 25 | } 26 | 27 | return result; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /tests/tests.pro: -------------------------------------------------------------------------------- 1 | QT += core concurrent network testlib 2 | QT -= gui 3 | 4 | TARGET = qt-async-tests 5 | 6 | CONFIG += console 7 | CONFIG -= app_bundle 8 | CONFIG += c++14 9 | 10 | TEMPLATE = app 11 | 12 | HEADERS += \ 13 | TestAsyncValue.h 14 | 15 | SOURCES += main.cpp \ 16 | TestAsyncValue.cpp 17 | 18 | INCLUDEPATH += ../qt-async-lib 19 | 20 | win32 { 21 | CONFIG(debug, debug|release): ASYNC_LIB_PATH = $$OUT_PWD/../qt-async-lib/debug 22 | CONFIG(release, debug|release): ASYNC_LIB_PATH = $$OUT_PWD/../qt-async-lib/release 23 | } else:unix { 24 | ASYNC_LIB_PATH = $$OUT_PWD/../qt-async-lib 25 | } 26 | 27 | LIBS += -L$$ASYNC_LIB_PATH -lqt-async-lib 28 | 29 | win32:PRE_TARGETDEPS += $$ASYNC_LIB_PATH/qt-async-lib.lib 30 | --------------------------------------------------------------------------------