FreeCAD

Форк
0
/
Control.cpp 
279 строк · 8.4 Кб
1
/***************************************************************************
2
 *   Copyright 2011 (c) Jürgen Riegel <juergen.riegel@web.de>              *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23

24
#include "PreCompiled.h"
25

26
#ifndef _PreComp_
27
# include <QAction>
28
# include <QApplication>
29
# include <QDebug>
30
# include <QDockWidget>
31
# include <QPointer>
32
#endif
33

34
#include <App/AutoTransaction.h>
35
#include <Gui/ComboView.h>
36
#include <Gui/DockWindowManager.h>
37
#include <Gui/MainWindow.h>
38

39
#include "Control.h"
40
#include "BitmapFactory.h"
41
#include "Tree.h"
42
#include "TaskView/TaskView.h"
43

44

45
using namespace Gui;
46
using namespace std;
47

48
/* TRANSLATOR Gui::ControlSingleton */
49

50
ControlSingleton* ControlSingleton::_pcSingleton = nullptr;
51

52
ControlSingleton::ControlSingleton()
53
  : ActiveDialog(nullptr)
54
  , oldTabIndex(-1)
55
{
56

57
}
58

59
ControlSingleton::~ControlSingleton() = default;
60

61
Gui::TaskView::TaskView* ControlSingleton::taskPanel() const
62
{
63
    auto taskView = qobject_cast<Gui::TaskView::TaskView*>
64
        (Gui::DockWindowManager::instance()->getDockWindow("Tasks"));
65
    return taskView;
66
}
67

68
void ControlSingleton::showDockWidget(QWidget* widget)
69
{
70
    QWidget* parent = widget->parentWidget();
71
    if (parent) {
72
        parent->show();
73
        parent->raise();
74
    }
75
}
76

77
QTabBar* ControlSingleton::findTabBar(QDockWidget* widget) const
78
{
79
    int count = getMainWindow()->tabifiedDockWidgets(widget).size() + 1;
80
    if (count > 1) {
81
        QList<QTabBar*> bars = getMainWindow()->findChildren<QTabBar*>();
82
        for (auto it : bars) {
83
            if (it->count() <= count) {
84
                for (int i = 0; i < count; i++) {
85
                    if (it->tabText(i) == widget->windowTitle()) {
86
                        return it;
87
                    }
88
                }
89
            }
90
        }
91
    }
92

93
    return nullptr;
94
}
95

96
void ControlSingleton::aboutToShowDialog(QDockWidget* widget)
97
{
98
    static QIcon icon = Gui::BitmapFactory().pixmap("edit-edit.svg");
99
    QTabBar* bar = findTabBar(widget);
100
    if (bar) {
101
        oldTabIndex = bar->currentIndex();
102
        for (int i = 0; i < bar->count(); i++) {
103
            if (bar->tabText(i) == widget->windowTitle()) {
104
                bar->setTabIcon(i, icon);
105
                break;
106
            }
107
        }
108
    }
109

110
    widget->show();
111
    widget->raise();
112
}
113

114
void ControlSingleton::aboutToHideDialog(QDockWidget* widget)
115
{
116
    QTabBar* bar = findTabBar(widget);
117
    if (bar) {
118
        bar->setCurrentIndex(oldTabIndex);
119
        for (int i = 0; i < bar->count(); i++) {
120
            if (bar->tabText(i) == widget->windowTitle()) {
121
                bar->setTabIcon(i, QIcon());
122
                break;
123
            }
124
        }
125
    }
126
}
127

128
void ControlSingleton::showTaskView()
129
{
130
    Gui::TaskView::TaskView* taskView = taskPanel();
131
    if (taskView) {
132
        showDockWidget(taskView);
133
    }
134
}
135

136
void ControlSingleton::showModelView()
137
{
138
    auto treeView = qobject_cast<Gui::TreeDockWidget*>
139
        (Gui::DockWindowManager::instance()->getDockWindow("Tree view"));
140
    if (treeView) {
141
        showDockWidget(treeView);
142
    }
143
}
144

145
void ControlSingleton::showDialog(Gui::TaskView::TaskDialog *dlg)
146
{
147
    // only one dialog at a time, print a warning instead of raising an assert
148
    if (ActiveDialog && ActiveDialog != dlg) {
149
        if (dlg) {
150
            qWarning() << "ControlSingleton::showDialog: Can't show "
151
                       << dlg->metaObject()->className()
152
                       << " since there is already an active task dialog";
153
        }
154
        else {
155
            qWarning() << "ControlSingleton::showDialog: Task dialog is null";
156
        }
157
        return;
158
    }
159

160
    // Since the caller sets up a modeless task panel, it indicates intention
161
    // for prolonged editing. So disable auto transaction in the current call
162
    // stack.
163
    // Do this before showing the dialog because its open() function is called
164
    // which may open a transaction but fails when auto transaction is still active.
165
    App::AutoTransaction::setEnable(false);
166

167
    Gui::TaskView::TaskView* taskView = taskPanel();
168
    // should return the pointer to combo view
169
    if (taskView) {
170
        taskView->showDialog(dlg);
171

172
        // make sure that the combo view is shown
173
        auto dw = qobject_cast<QDockWidget*>(taskView->parentWidget());
174
        if (dw) {
175
            aboutToShowDialog(dw);
176
            dw->setVisible(true);
177
            dw->toggleViewAction()->setVisible(true);
178
            dw->setFeatures(QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetFloatable);
179
        }
180

181
        if (ActiveDialog == dlg)
182
            return; // dialog is already defined
183
        ActiveDialog = dlg;
184
        connect(dlg, &TaskView::TaskDialog::aboutToBeDestroyed,
185
                this, &ControlSingleton::closedDialog);
186
    }
187
}
188

189
Gui::TaskView::TaskDialog* ControlSingleton::activeDialog() const
190
{
191
    return ActiveDialog;
192
}
193

194
void ControlSingleton::accept()
195
{
196
    Gui::TaskView::TaskView* taskView = taskPanel();
197
    if (taskView) {
198
        taskView->accept();
199
        qApp->processEvents(QEventLoop::ExcludeUserInputEvents |
200
                            QEventLoop::ExcludeSocketNotifiers);
201
    }
202
}
203

204
void ControlSingleton::reject()
205
{
206
    Gui::TaskView::TaskView* taskView = taskPanel();
207
    if (taskView) {
208
        taskView->reject();
209
        qApp->processEvents(QEventLoop::ExcludeUserInputEvents |
210
                            QEventLoop::ExcludeSocketNotifiers);
211
    }
212
}
213

214
void ControlSingleton::closeDialog()
215
{
216
    Gui::TaskView::TaskView* taskView = taskPanel();
217
    if (taskView)
218
        taskView->removeDialog();
219
}
220

221
void ControlSingleton::closedDialog()
222
{
223
    ActiveDialog = nullptr;
224
    Gui::TaskView::TaskView* taskView = taskPanel();
225
    assert(taskView);
226

227
    // make sure that the combo view is shown
228
    auto dw = qobject_cast<QDockWidget*>(taskView->parentWidget());
229
    if (dw) {
230
        aboutToHideDialog(dw);
231
        dw->setFeatures(QDockWidget::DockWidgetClosable
232
                        | QDockWidget::DockWidgetMovable
233
                        | QDockWidget::DockWidgetFloatable);
234
    }
235
}
236

237
bool ControlSingleton::isAllowedAlterDocument() const
238
{
239
    if (ActiveDialog)
240
        return ActiveDialog->isAllowedAlterDocument();
241
    return true;
242
}
243

244

245
bool ControlSingleton::isAllowedAlterView() const
246
{
247
    if (ActiveDialog)
248
        return ActiveDialog->isAllowedAlterView();
249
    return true;
250
}
251

252
bool ControlSingleton::isAllowedAlterSelection() const
253
{
254
    if (ActiveDialog)
255
        return ActiveDialog->isAllowedAlterSelection();
256
    return true;
257
}
258

259
// -------------------------------------------
260

261
ControlSingleton& ControlSingleton::instance()
262
{
263
    if (!_pcSingleton)
264
        _pcSingleton = new ControlSingleton;
265
    return *_pcSingleton;
266
}
267

268
void ControlSingleton::destruct ()
269
{
270
    if (_pcSingleton)
271
        delete _pcSingleton;
272
    _pcSingleton = nullptr;
273
}
274

275

276
// -------------------------------------------
277

278

279
#include "moc_Control.cpp"
280

281

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.