Changeset View
Changeset View
Standalone View
Standalone View
util/scopeddialog.h
- This file was added.
1 | /* This file is part of KDevelop | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * Copyright 2017 Christoph Roick <chrisito@gmx.de> | ||||
4 | * | ||||
5 | * This program is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU General Public License | ||||
7 | * as published by the Free Software Foundation; either version 2 | ||||
8 | * of the License, or (at your option) any later version. | ||||
9 | * | ||||
10 | * This program is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | * GNU General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU General Public License | ||||
16 | * along with this program; if not, write to the Free Software | ||||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||
18 | * 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #ifndef KDEVPLATFORM_SCOPEDDIALOG_H | ||||
22 | #define KDEVPLATFORM_SCOPEDDIALOG_H | ||||
23 | | ||||
24 | #include <QPointer> | ||||
25 | | ||||
26 | namespace KDevelop { | ||||
27 | | ||||
28 | /** | ||||
29 | * Wrapper class for QDialogs which should not be instantiated on stack. | ||||
30 | * | ||||
31 | * Parents of QDialogs may be unintentionally deleted during the execution of the | ||||
32 | * dialog and automatically delete their children. When returning to the calling | ||||
33 | * function they get intentionally deleted again, which will lead to a crash. This | ||||
34 | * can be circumvented by using a QPointer which keeps track of the QDialogs validity. | ||||
35 | * See this | ||||
36 | * <a href="https://blogs.kde.org/2009/03/26/how-crash-almost-every-qtkde-application-and-how-fix-it-0">blog entry</a> | ||||
37 | * for explanation. The ScopedDialog utility allows using the dialog like a | ||||
38 | * common pointer. | ||||
39 | * | ||||
40 | * Instead of | ||||
41 | * \code | ||||
42 | QFileDialog dlg(this); | ||||
43 | if (dlg.exec()) | ||||
44 | return; | ||||
45 | \endcode | ||||
46 | simply use | ||||
47 | * \code | ||||
48 | ScopedDialog<QFileDialog> dlg(this); | ||||
49 | if (dlg->exec()) | ||||
50 | return; | ||||
51 | \endcode | ||||
52 | without need to manually clean up afterwards. | ||||
53 | */ | ||||
54 | template<typename DialogType> | ||||
55 | class ScopedDialog { | ||||
56 | public: | ||||
57 | /// Construct the dialog with any set of allowed arguments | ||||
58 | /// for the construction of DialogType | ||||
59 | template<typename ... Arguments> | ||||
60 | explicit ScopedDialog(Arguments ... args) : ptr(new DialogType(args...)) { | ||||
61 | } | ||||
62 | /// Automatically deletes the dialog if it is still present | ||||
63 | ~ScopedDialog() { | ||||
64 | delete ptr; | ||||
65 | } | ||||
66 | | ||||
67 | /// Access members of the dialog | ||||
68 | DialogType* operator->() const { | ||||
69 | return ptr; | ||||
70 | } | ||||
71 | /// Access the dialog | ||||
72 | DialogType & operator*() const { | ||||
73 | return *ptr; | ||||
74 | } | ||||
75 | /// Return the corresponding pointer | ||||
76 | operator DialogType*() const { | ||||
77 | return ptr; | ||||
78 | } | ||||
79 | | ||||
80 | private: | ||||
81 | QPointer<DialogType> ptr; | ||||
82 | }; | ||||
83 | | ||||
84 | } | ||||
85 | | ||||
86 | #endif // KDEVPLATFORM_SCOPEDDIALOG_H |