FreeCAD

Форк
0
/
Control.cpp 
286 строк · 8.6 Кб
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
    else {
144
        auto comboView = qobject_cast<Gui::DockWnd::ComboView*>
145
            (Gui::DockWindowManager::instance()->getDockWindow("Model"));
146
        if (comboView) {
147
            showDockWidget(comboView);
148
        }
149
    }
150
}
151

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

167
    // Since the caller sets up a modeless task panel, it indicates intention
168
    // for prolonged editing. So disable auto transaction in the current call
169
    // stack.
170
    // Do this before showing the dialog because its open() function is called
171
    // which may open a transaction but fails when auto transaction is still active.
172
    App::AutoTransaction::setEnable(false);
173

174
    Gui::TaskView::TaskView* taskView = taskPanel();
175
    // should return the pointer to combo view
176
    if (taskView) {
177
        taskView->showDialog(dlg);
178

179
        // make sure that the combo view is shown
180
        auto dw = qobject_cast<QDockWidget*>(taskView->parentWidget());
181
        if (dw) {
182
            aboutToShowDialog(dw);
183
            dw->setVisible(true);
184
            dw->toggleViewAction()->setVisible(true);
185
            dw->setFeatures(QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetFloatable);
186
        }
187

188
        if (ActiveDialog == dlg)
189
            return; // dialog is already defined
190
        ActiveDialog = dlg;
191
        connect(dlg, &TaskView::TaskDialog::aboutToBeDestroyed,
192
                this, &ControlSingleton::closedDialog);
193
    }
194
}
195

196
Gui::TaskView::TaskDialog* ControlSingleton::activeDialog() const
197
{
198
    return ActiveDialog;
199
}
200

201
void ControlSingleton::accept()
202
{
203
    Gui::TaskView::TaskView* taskView = taskPanel();
204
    if (taskView) {
205
        taskView->accept();
206
        qApp->processEvents(QEventLoop::ExcludeUserInputEvents |
207
                            QEventLoop::ExcludeSocketNotifiers);
208
    }
209
}
210

211
void ControlSingleton::reject()
212
{
213
    Gui::TaskView::TaskView* taskView = taskPanel();
214
    if (taskView) {
215
        taskView->reject();
216
        qApp->processEvents(QEventLoop::ExcludeUserInputEvents |
217
                            QEventLoop::ExcludeSocketNotifiers);
218
    }
219
}
220

221
void ControlSingleton::closeDialog()
222
{
223
    Gui::TaskView::TaskView* taskView = taskPanel();
224
    if (taskView)
225
        taskView->removeDialog();
226
}
227

228
void ControlSingleton::closedDialog()
229
{
230
    ActiveDialog = nullptr;
231
    Gui::TaskView::TaskView* taskView = taskPanel();
232
    assert(taskView);
233

234
    // make sure that the combo view is shown
235
    auto dw = qobject_cast<QDockWidget*>(taskView->parentWidget());
236
    if (dw) {
237
        aboutToHideDialog(dw);
238
        dw->setFeatures(QDockWidget::DockWidgetClosable
239
                        | QDockWidget::DockWidgetMovable
240
                        | QDockWidget::DockWidgetFloatable);
241
    }
242
}
243

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

251

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

259
bool ControlSingleton::isAllowedAlterSelection() const
260
{
261
    if (ActiveDialog)
262
        return ActiveDialog->isAllowedAlterSelection();
263
    return true;
264
}
265

266
// -------------------------------------------
267

268
ControlSingleton& ControlSingleton::instance()
269
{
270
    if (!_pcSingleton)
271
        _pcSingleton = new ControlSingleton;
272
    return *_pcSingleton;
273
}
274

275
void ControlSingleton::destruct ()
276
{
277
    if (_pcSingleton)
278
        delete _pcSingleton;
279
    _pcSingleton = nullptr;
280
}
281

282

283
// -------------------------------------------
284

285

286
#include "moc_Control.cpp"
287

288

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

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

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

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