File: pqMainWindow.cpp

package info (click to toggle)
swi-prolog 9.2.9%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 84,456 kB
  • sloc: ansic: 401,705; perl: 374,799; lisp: 9,080; cpp: 8,920; java: 5,525; sh: 3,282; javascript: 2,690; python: 2,655; ruby: 1,594; yacc: 845; makefile: 440; xml: 317; sed: 12; sql: 6
file content (219 lines) | stat: -rw-r--r-- 6,523 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*  Part of SWI-Prolog interface to Qt

    Author:        Carlo Capelli
    E-mail:        cc.carlo.cap@gmail.com
    Copyright (c)  2013-2015, Carlo Capelli
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

#include "pqMainWindow.h"
#include "ConsoleEdit.h"
#include "Preferences.h"
#include "PREDICATE.h"
#include "do_events.h"

#include <QMenu>
#include <QDebug>
#include <QTimer>
#include <QMessageBox>
#include <QApplication>
#include <QMenuBar>

inline ConsoleEdit *wid2con(QWidget *w) { return qobject_cast<ConsoleEdit*>(w); }

pqMainWindow::pqMainWindow(QWidget *parent) :
    QMainWindow(parent) {
#ifndef __APPLE__
    QMenuBar *mb = menuBar();
    mb->setNativeMenuBar(false);
#endif
}

/** this is the mandatory constructor to get SWI-prolog embedding
 *  and proper XPCE termination
 */
pqMainWindow::pqMainWindow(int argc, char *argv[]) {
#ifndef __APPLE__
    QMenuBar *mb = menuBar();
    mb->setNativeMenuBar(false);
#endif

    setCentralWidget(new ConsoleEdit(argc, argv));

    Preferences p;
    p.loadGeometry(this);
}

/** handle application closing, WRT XPCE termination
 */
void pqMainWindow::closeEvent(QCloseEvent *event) {
    auto t = consoles();
    if (t) {
        for (int c = 0; c < t->count(); ++c)
            if (!wid2con(t->widget(c))->can_close()) {
                event->ignore();
                return;
            }
    }
    else if (!wid2con(centralWidget())->can_close()) {
        event->ignore();
        return;
    }

    {   Preferences p;
        p.saveGeometry(this);
    }

    if (!SwiPrologEngine::quit_request())
        event->ignore();
else
    qApp->quit();
}

/** pass initialization script to actual interface
 */
void pqMainWindow::set_script(QString name, QString text) {
    console()->engine()->script_run(name, text);
}

/** get access to the widget
 */
ConsoleEdit *pqMainWindow::console(int thread) const {

    if (!consoles()) {
        // don't search
        auto c = wid2con(centralWidget());
        return c->match_thread(thread) ? c : 0;
    }

    for (int i = 0; consoles()->count(); ++i) {
        auto c = wid2con(consoles()->widget(i));
        if (c->match_thread(thread))
            return c;
    }

    return 0;
}

/** get current active widget
 */
ConsoleEdit *pqMainWindow::consoleActive() const {
    auto t = consoles();
    QWidget *w = t ? t->currentWidget() : centralWidget();
    Q_ASSERT(w && wid2con(w));
    return wid2con(w);
}

/** qualify widget type
 */
QTabWidget *pqMainWindow::consoles() const {
    return qobject_cast<QTabWidget*>(centralWidget());
}

/** switch the interface to tabbed on creation of second (and subsequent) console
 */
void pqMainWindow::addConsole(ConsoleEdit *console, QString title) {
    auto t = consoles();
    if (!t) {
        auto c = wid2con(centralWidget());
        t = new QTabWidget;
        t->setTabsClosable(true);
        QString T = windowTitle();
        if (T.isEmpty())
            T = "Main";
        t->addTab(c, T);
        setCentralWidget(t);
        connect(t, SIGNAL(tabCloseRequested(int)), this, SLOT(tabCloseRequested(int)));
    }
    int i = t->addTab(console, title);
    t->setCurrentIndex(i);
    console->setFocus();
}

/** handle the close button, issuing console request and removing from tab
 */
void pqMainWindow::tabCloseRequested(int tabId) {
    auto c = wid2con(consoles()->widget(tabId));
    if (c->can_close()) {
        if (tabId == 0) {
            QMessageBox b(this);
            b.setWindowTitle(tr("Cannot close"));
            b.setText(tr("[%1] is the primary console.\nDo you want to quit?").arg(c->titleLabel()));
            b.setIcon(b.Question);
            b.setStandardButtons(b.Yes | b.No);
            if (b.exec() == b.Yes)
                qApp->quit();
            return;
        }
        consoles()->removeTab(tabId);
    }
}

/** close console by object
 */
void pqMainWindow::remConsole(ConsoleEdit *c) {
    if (auto t = consoles())
        if (t->indexOf(c) > 0)
            t->removeTab(t->indexOf(c));
}

/** handle menu dispatch by means of Qt signal mapper
 */
void pqMainWindow::addActionPq(ConsoleEdit *ce, QMenu *cmmenu, QString label, QString action) {
    // dispatch signals indexed
    QAction *a = cmmenu->addAction(label, menuMapper(ce), SLOT(map()));
    menu2pl->setMapping(a, action);
}

/** ditto
 */
QAction* pqMainWindow::add_action(ConsoleEdit *ce, QMenu *mn, QString Label, QString ctxtmod, QString Goal, QAction *before) {
    QAction *a;
    if (!before)
        a = mn->addAction(Label, menuMapper(ce), SLOT(map()));
    else {
        mn->insertAction(before, a = new QAction(Label, mn));
        connect(a, SIGNAL(triggered()), menuMapper(ce), SLOT(map()));
    }
    menu2pl->setMapping(a, ctxtmod + ':' + Goal);
    return a;
}

/** 20/12/2021 - factorize code subject to Qt versioning
 */
QSignalMapper* pqMainWindow::menuMapper(ConsoleEdit *ce) {
    if (!menu2pl) {
        menu2pl = new QSignalMapper;
#if QT_VERSION < 0x051500
        connect(menu2pl, SIGNAL(mapped(const QString &)), ce, SLOT(onConsoleMenuActionMap(const QString &)));
#else
        connect(menu2pl, &QSignalMapper::mappedString, ce, &ConsoleEdit::onConsoleMenuActionMap);
#endif
    }
    return menu2pl;
}