cantor.cpp 23.7 KB
Newer Older
1
/*
Alexander Rieder's avatar
Alexander Rieder committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA  02110-1301, USA.

    ---
    Copyright (C) 2009 Alexander Rieder <alexanderrieder@gmail.com>
19
 */
Alexander Rieder's avatar
Alexander Rieder committed
20
#include "cantor.h"
21

22
#include <KActionCollection>
23
24
#include <KConfigDialog>
#include <KConfigGroup>
25
#include <KMessageBox>
26
#include <KShortcutsDialog>
27
28
#include <KStandardAction>
#include <KNS3/DownloadDialog>
29
#include <KParts/ReadWritePart>
30
#include <KRecentFilesAction>
31

32
#include <QApplication>
33
#include <QCloseEvent>
34
35
#include <QDebug>
#include <QDockWidget>
36
#include <QDir>
37
#include <QFileDialog>
38
#include <QPushButton>
39
#include <QGraphicsView>
40
41

#include "lib/backend.h"
42
43
#include "lib/panelpluginhandler.h"
#include "lib/panelplugin.h"
44
#include "lib/worksheetaccess.h"
45
46
47

#include "settings.h"
#include "ui_settings.h"
48
#include "backendchoosedialog.h"
49

50
CantorShell::CantorShell() : KParts::MainWindow(), m_part(nullptr)
51
52
{
    // set the shell's ui resource file
53
    setXMLFile(QLatin1String("cantor_shell.rc"));
54
55
56
57

    // then, setup our actions
    setupActions();

58
    createGUI(nullptr);
59

Filipe Saraiva's avatar
Filipe Saraiva committed
60
    m_tabWidget=new QTabWidget(this);
61
    m_tabWidget->setTabsClosable(true);
62
63
    m_tabWidget->setMovable(true);
    m_tabWidget->setDocumentMode(true);
64
65
66
    setCentralWidget(m_tabWidget);

    connect(m_tabWidget, SIGNAL(currentChanged(int)), this, SLOT(activateWorksheet(int)));
67
    connect(m_tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
68
69
70
71
72

    // apply the saved mainwindow settings, if any, and ask the mainwindow
    // to automatically save settings if changed: window size, toolbar
    // position, icon size, etc.
    setAutoSaveSettings();
73
74

    setDockOptions(QMainWindow::AnimatedDocks|QMainWindow::AllowTabbedDocks|QMainWindow::VerticalTabs);
75
76

    updateNewSubmenu();
77
78
}

79
80
81
82
CantorShell::~CantorShell()
{
    if (m_recentProjectsAction)
        m_recentProjectsAction->saveEntries(KSharedConfig::openConfig()->group(QLatin1String("Recent Files")));
83
84
85
86
87
88
89
90
91
92
93

    if (!m_newBackendActions.isEmpty())
    {
        unplugActionList(QLatin1String("new_worksheet_with_backend_list"));
        qDeleteAll(m_newBackendActions);
        m_newBackendActions.clear();
    }

    unplugActionList(QLatin1String("view_show_panel_list"));
    qDeleteAll(m_panels);
    m_panels.clear();
94
95
}

96
void CantorShell::load(const QUrl &url)
97
{
Alexander Rieder's avatar
Alexander Rieder committed
98
    if (!m_part||!m_part->url().isEmpty() || m_part->isModified() )
99
    {
100
        addWorksheet(QString());
101
102
        m_tabWidget->setCurrentIndex(m_parts.size()-1);
    }
103
104
    if (!m_part->openUrl( url ))
        closeTab(m_tabWidget->currentIndex());
105
106
    if (m_recentProjectsAction)
        m_recentProjectsAction->addUrl(url);
107
108
}

Alexander Rieder's avatar
Alexander Rieder committed
109
void CantorShell::setupActions()
110
{
111
    QAction* openNew = KStandardAction::openNew(this, SLOT(fileNew()), actionCollection());
112
    openNew->setPriority(QAction::LowPriority);
113
    QAction* open = KStandardAction::open(this, SLOT(fileOpen()), actionCollection());
114
    open->setPriority(QAction::LowPriority);
115
116
117
    m_recentProjectsAction = KStandardAction::openRecent(this, &CantorShell::load, actionCollection());
    m_recentProjectsAction->setPriority(QAction::LowPriority);
    m_recentProjectsAction->loadEntries(KSharedConfig::openConfig()->group(QLatin1String("Recent Files")));
118

119
120
    KStandardAction::close (this,  SLOT(closeTab()),  actionCollection());

121
122
123
124
125
    KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollection());

    createStandardStatusBarAction();
    //setStandardToolBarMenuEnabled(true);

126
127
    KStandardAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
    KStandardAction::configureToolbars(this, SLOT(configureToolbars()), actionCollection());
128
129

    KStandardAction::preferences(this, SLOT(showSettings()), actionCollection());
130

131
132
133
    QAction * downloadExamples = new QAction(i18n("Download Example Worksheets"), actionCollection());
    downloadExamples->setIcon(QIcon::fromTheme(QLatin1String("get-hot-new-stuff")));
    actionCollection()->addAction(QLatin1String("file_example_download"),  downloadExamples);
134
    connect(downloadExamples, SIGNAL(triggered()), this,  SLOT(downloadExamples()));
135

136
137
138
    QAction * openExample =new QAction(i18n("&Open Example"), actionCollection());
    openExample->setIcon(QIcon::fromTheme(QLatin1String("document-open")));
    actionCollection()->addAction(QLatin1String("file_example_open"), openExample);
139
    connect(openExample, SIGNAL(triggered()), this, SLOT(openExample()));
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

    QAction* toPreviousTab = new QAction(i18n("Go to previous worksheet"), actionCollection());
    actionCollection()->addAction(QLatin1String("go_to_previous_tab"), toPreviousTab);
    actionCollection()->setDefaultShortcut(toPreviousTab, Qt::CTRL+Qt::Key_PageDown);
    connect(toPreviousTab, &QAction::triggered, toPreviousTab, [this](){
        const int index = m_tabWidget->currentIndex()-1;
        if (index >= 0)
            m_tabWidget->setCurrentIndex(index);
        else
            m_tabWidget->setCurrentIndex(m_tabWidget->count()-1);
    });
    addAction(toPreviousTab);

    QAction* toNextTab = new QAction(i18n("Go to next worksheet"), actionCollection());
    actionCollection()->addAction(QLatin1String("go_to_next_tab"), toNextTab);
    actionCollection()->setDefaultShortcut(toNextTab, Qt::CTRL+Qt::Key_PageUp);
    connect(toNextTab, &QAction::triggered, toNextTab, [this](){
        const int index = m_tabWidget->currentIndex()+1;
        if (index < m_tabWidget->count())
            m_tabWidget->setCurrentIndex(index);
        else
            m_tabWidget->setCurrentIndex(0);
    });
    addAction(toNextTab);
164
165
}

Alexander Rieder's avatar
Alexander Rieder committed
166
void CantorShell::saveProperties(KConfigGroup & /*config*/)
167
168
169
170
171
172
{
    // the 'config' object points to the session managed
    // config file.  anything you write here will be available
    // later when this app is restored
}

Alexander Rieder's avatar
Alexander Rieder committed
173
void CantorShell::readProperties(const KConfigGroup & /*config*/)
174
175
176
177
178
179
180
{
    // the 'config' object points to the session managed
    // config file.  this function is automatically called whenever
    // the app is being restored.  read in here whatever you wrote
    // in 'saveProperties'
}

181
182
183
184
185
/*!
 * called when one of the "new backend" action or the "New" action are called
 * adds a new worksheet with the backend assossiated with the called action
 * or opens the "Choose Backend" dialog, respectively.
 */
Alexander Rieder's avatar
Alexander Rieder committed
186
void CantorShell::fileNew()
187
{
188
189
190
    QAction* a = static_cast<QAction*>(sender());
    const QString& backendName = a->data().toString();
    if (!backendName.isEmpty())
191
    {
192
193
        addWorksheet(backendName);
        return;
194
    }
195
196

    //"New" action was called -> open the "Choose Backend" dialog.
197
    addWorksheet();
198
199
}

Alexander Rieder's avatar
Alexander Rieder committed
200
void CantorShell::optionsConfigureKeys()
201
{
202
203
    KShortcutsDialog dlg( KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsDisallowed, this );
    dlg.addCollection( actionCollection(), i18n("Cantor") );
204
205
    if (m_part)
        dlg.addCollection( m_part->actionCollection(), i18n("Cantor") );
206
    dlg.configure( true );
207
208
}

Alexander Rieder's avatar
Alexander Rieder committed
209
void CantorShell::fileOpen()
210
211
212
213
{
    // this slot is called whenever the File->Open menu is selected,
    // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
    // button is clicked
214
215
216
217
218
219
220
221
    static const QString& worksheetFilter = i18n("Cantor Worksheet (*.cws)");
    static const QString& notebookFilter = i18n("Jupyter Notebook (*.ipynb)");
    QString filter;
    if (m_previousFilter == notebookFilter)
        filter = notebookFilter + QLatin1String(";;") + worksheetFilter;
    else
        filter = worksheetFilter + QLatin1String(";;") + notebookFilter;
    QUrl url = QFileDialog::getOpenFileUrl(this, i18n("Open file"), QUrl(), filter, &m_previousFilter);
222
223
224

    if (url.isEmpty() == false)
    {
225
        // About this function, the style guide
226
227
228
229
230
231
232
233
234
235
        // says that it should open a new window if the document is _not_
        // in its initial state.  This is what we do here..
        /*if ( m_part->url().isEmpty() && ! m_part->isModified() )
        {
            // we open the file in this window...
            load( url );
        }
        else
        {
            // we open the file in a new window...
Alexander Rieder's avatar
Alexander Rieder committed
236
            CantorShell* newWin = new CantorShell;
237
238
239
240
241
242
243
            newWin->load( url );
            newWin->show();
            }*/
        load( url );
    }
}

Alexander Rieder's avatar
Alexander Rieder committed
244
void CantorShell::addWorksheet()
245
{
246
247
248
249
250
251
252
253
254
255
256
    bool hasBackend = false;
    for (auto* b : Cantor::Backend::availableBackends())
    {
        if(b->isEnabled())
        {
            hasBackend = true;
            break;
        }
    }

    if(hasBackend) //There is no point in asking for the backend, if no one is available
257
    {
258
259
        QString backend = Settings::self()->defaultBackend();
        if (backend.isEmpty())
260
        {
261
262
263
264
            QPointer<BackendChooseDialog> dlg=new BackendChooseDialog(this);
            if(dlg->exec())
            {
                backend = dlg->backendName();
265
                addWorksheet(backend);
266
267
268
            }

            delete dlg;
269
        }
270
271
272
273
        else
        {
            addWorksheet(backend);
        }
274

275
276
    }else
    {
277
        QTextBrowser *browser=new QTextBrowser(this);
278
        QString backendList=QLatin1String("<ul>");
Andrew Coles's avatar
Andrew Coles committed
279
        int backendListSize = 0;
280
281
        foreach(Cantor::Backend* b, Cantor::Backend::availableBackends())
        {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
282
            if(!b->requirementsFullfilled()) //It's disabled because of missing dependencies, not because of some other reason(like eg. nullbackend)
Andrew Coles's avatar
Andrew Coles committed
283
            {
284
                backendList+=QString::fromLatin1("<li>%1: <a href=\"%2\">%2</a></li>").arg(b->name(), b->url());
Andrew Coles's avatar
Andrew Coles committed
285
286
                ++backendListSize;
            }
287
        }
Andrew Coles's avatar
Andrew Coles committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
        browser->setHtml(i18np("<h1>No Backend Found</h1>\n"             \
                               "<div>You could try:\n"                   \
                               "  <ul>"                                  \
                               "    <li>Changing the settings in the config dialog;</li>" \
                               "    <li>Installing packages for the following program:</li>" \
                               "     %2 "                                \
                               "  </ul> "                                 \
                               "</div> "
                              , "<h1>No Backend Found</h1>\n"             \
                               "<div>You could try:\n"                   \
                               "  <ul>"                                  \
                               "    <li>Changing the settings in the config dialog;</li>" \
                               "    <li>Installing packages for one of the following programs:</li>" \
                               "     %2 "                                \
                               "  </ul> "                                 \
                               "</div> "
                              , backendListSize, backendList
                              ));
306

307
        browser->setObjectName(QLatin1String("ErrorMessage"));
308
309
        m_tabWidget->addTab(browser, i18n("Error"));
    }
310
311
}

Alexander Rieder's avatar
Alexander Rieder committed
312
void CantorShell::addWorksheet(const QString& backendName)
313
314
315
316
317
318
{
    static int sessionCount=1;

    // this routine will find and load our Part.  it finds the Part by
    // name which is a bad idea usually.. but it's alright in this
    // case since our Part is made for this Shell
319
320
    KPluginLoader loader(QLatin1String("cantorpart"));
    KPluginFactory* factory = loader.factory();
321
322
    if (factory)
    {
323
        if (!backendName.isEmpty())
324
        {
325
326
            Cantor::Backend* backend = Cantor::Backend::getBackend(backendName);
            if (!backend)
327
            {
328
329
                KMessageBox::error(this, i18n("Backend %1 is not installed", backendName), i18n("Cantor"));
                return;
330
            }
331
            else
332
            {
333
334
335
336
337
                if (!backend->isEnabled())
                {
                    KMessageBox::error(this, i18n("%1 backend installed, but inactive. Please check installation and Cantor settings", backendName), i18n("Cantor"));
                    return;
                }
338
            }
339
        }
340
341
342
343
344
345
346
347
348
349
350
351
352
353

        // now that the Part is loaded, we cast it to a Part to get our hands on it
        KParts::ReadWritePart* part = factory->create<KParts::ReadWritePart>(m_tabWidget, QVariantList()<<backendName);
        if (part)
        {
            connect(part, SIGNAL(setCaption(QString,QIcon)), this, SLOT(setTabCaption(QString,QIcon)));
            connect(part, SIGNAL(worksheetSave(QUrl)), this, SLOT(onWorksheetSave(QUrl)));
            m_parts.append(part);

            int tab = m_tabWidget->addTab(part->widget(), i18n("Session %1", sessionCount++));
            m_tabWidget->setCurrentIndex(tab);
            // Setting focus on worksheet view, because Qt clear focus of added widget inside addTab
            // This fix https://bugs.kde.org/show_bug.cgi?id=395976
            part->widget()->findChild<QGraphicsView*>()->setFocus();
354
355
356

            // Force run updateCaption for getting proper backend icon
            QMetaObject::invokeMethod(part, "updateCaption");
357
        }
358
        else
359
360
361
        {
            qDebug()<<"error creating part ";
        }
362
363
364
365
366
    }
    else
    {
        // if we couldn't find our Part, we exit since the Shell by
        // itself can't do anything useful
367
        KMessageBox::error(this, i18n("Failed to find the Cantor Part with error %1", loader.errorString()));
368
369
370
371
372
373
374
375
        qApp->quit();
        // we return here, cause qApp->quit() only means "exit the
        // next time we enter the event loop...
        return;
    }

}

Alexander Rieder's avatar
Alexander Rieder committed
376
void CantorShell::activateWorksheet(int index)
377
{
378
    QObject* pluginHandler=m_part->findChild<QObject*>(QLatin1String("PanelPluginHandler"));
379
380
381
382
383
384
385
386
387
388
389
390
391
392
    if (pluginHandler)
        disconnect(pluginHandler,SIGNAL(pluginsChanged()), this, SLOT(updatePanel()));

    // Save part state before change worksheet
    if (m_part)
    {
        QStringList visiblePanelNames;
        foreach (QDockWidget* doc, m_panels)
        {
            if (doc->widget() && doc->widget()->isVisible())
                visiblePanelNames << doc->objectName();
        }
        m_pluginsVisibility[m_part] = visiblePanelNames;
    }
393

394
    m_part=findPart(m_tabWidget->widget(index));
395
    if(m_part)
396
    {
397
        createGUI(m_part);
398

399
        QObject* pluginHandler=m_part->findChild<QObject*>(QLatin1String("PanelPluginHandler"));
400
401
402
        connect(pluginHandler, SIGNAL(pluginsChanged()), this, SLOT(updatePanel()));
        updatePanel();
    }
403
    else
404
        qDebug()<<"selected part doesn't exist";
405
406

    m_tabWidget->setCurrentIndex(index);
407
408
}

409
void CantorShell::setTabCaption(const QString& caption, const QIcon& icon)
410
411
{
    KParts::ReadWritePart* part=dynamic_cast<KParts::ReadWritePart*>(sender());
412
413
    if (part)
    {
414
415
        if (!caption.isEmpty())
            m_tabWidget->setTabText(m_tabWidget->indexOf(part->widget()), caption);
416
417
        m_tabWidget->setTabIcon(m_tabWidget->indexOf(part->widget()), icon);
    }
418
419
}

420
void CantorShell::closeTab(int index)
421
{
422
423
    if (!reallyClose(false))
    {
424
425
        return;
    }
426
427
428

    QWidget* widget = nullptr;
    if (index >= 0)
429
    {
430
431
432
433
434
435
436
437
438
439
440
        widget = m_tabWidget->widget(index);
    }
    else if (m_part)
    {
        widget = m_part->widget();
    }

    if (!widget)
    {
        qWarning() << "Could not find widget by tab index" << index;
        return;
441
    }
442

443

444
445
    m_tabWidget->removeTab(index);

446
    if(widget->objectName()==QLatin1String("ErrorMessage"))
447
448
449
450
451
452
453
454
    {
        widget->deleteLater();
    }else
    {
        KParts::ReadWritePart* part= findPart(widget);
        if(part)
        {
            m_parts.removeAll(part);
455
            m_pluginsVisibility.remove(part);
456
            delete part;
457
458
        }
    }
459
460
461

    if (m_tabWidget->count() == 0)
        setCaption(QString());
462
    updatePanel();
463
464
}

465
466
467
bool CantorShell::reallyClose(bool checkAllParts) {
    if(checkAllParts && m_parts.count() > 1) {
        bool modified = false;
468
        foreach( KParts::ReadWritePart* const part, m_parts)
469
470
471
472
473
474
475
476
        {
            if(part->isModified()) {
                modified = true;
                break;
            }
        }
        if(!modified) return true;
        int want_save = KMessageBox::warningYesNo( this,
477
            i18n("Multiple unsaved Worksheets are opened. Do you want to close them?"),
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
            i18n("Close Cantor"));
        switch (want_save) {
            case KMessageBox::Yes:
                return true;
            case KMessageBox::No:
                return false;
        }
    }
    if (m_part && m_part->isModified() ) {
        int want_save = KMessageBox::warningYesNoCancel( this,
            i18n("The current project has been modified. Do you want to save it?"),
            i18n("Save Project"));
        switch (want_save) {
            case KMessageBox::Yes:
                m_part->save();
                if(m_part->waitSaveComplete()) {
                    return true;
                } else {
                    m_part->setModified(true);
                    return false;
                }
            case KMessageBox::Cancel:
                return false;
            case KMessageBox::No:
                return true;
        }
    }
    return true;
}

void CantorShell::closeEvent(QCloseEvent* event) {
    if(!reallyClose()) {
        event->ignore();
    } else {
        KParts::MainWindow::closeEvent(event);
    }
}

Alexander Rieder's avatar
Alexander Rieder committed
516
void CantorShell::showSettings()
517
{
518
    KConfigDialog *dialog = new KConfigDialog(this,  QLatin1String("settings"), Settings::self());
519
520
521
    QWidget *generalSettings = new QWidget;
    Ui::SettingsBase base;
    base.setupUi(generalSettings);
Alexander Rieder's avatar
Alexander Rieder committed
522
    base.kcfg_DefaultBackend->addItems(Cantor::Backend::listAvailableBackends());
523

524
    dialog->addPage(generalSettings, i18n("General"), QLatin1String("preferences-other"));
Alexander Rieder's avatar
Alexander Rieder committed
525
    foreach(Cantor::Backend* backend, Cantor::Backend::availableBackends())
526
527
528
529
530
531
532
    {
        if (backend->config()) //It has something to configure, so add it to the dialog
            dialog->addPage(backend->settingsWidget(dialog), backend->config(), backend->name(),  backend->icon());
    }

    dialog->show();
}
533

Alexander Rieder's avatar
Alexander Rieder committed
534
void CantorShell::downloadExamples()
535
{
536
537
538
    KNS3::DownloadDialog dialog;
    dialog.exec();
    foreach (const KNS3::Entry& e,  dialog.changedEntries())
539
    {
540
        qDebug() << "Changed Entry: " << e.name();
541
542
    }
}
543

Alexander Rieder's avatar
Alexander Rieder committed
544
void CantorShell::openExample()
545
{
546
    QString dir = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1String("/examples");
547
    if (dir.isEmpty()) return;
548
    QDir().mkpath(dir);
549
550

    QStringList files=QDir(dir).entryList(QDir::Files);
551
    QPointer<QDialog> dlg=new QDialog(this);
552
    QListWidget* list=new QListWidget(dlg);
553
554
555
    foreach(const QString& file, files)
    {
        QString name=file;
556
        name.remove(QRegExp(QLatin1String("-.*\\.hotstuff-access$")));
557
558
559
        list->addItem(name);
    }

560
561
562
563
564
565
566
567
568
569
570
571
572
    QVBoxLayout *mainLayout = new QVBoxLayout;
    dlg->setLayout(mainLayout);
    mainLayout->addWidget(list);

    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);

    mainLayout->addWidget(buttonBox);

    buttonBox->button(QDialogButtonBox::Ok)->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogOkButton));
    buttonBox->button(QDialogButtonBox::Cancel)->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogCancelButton));

    connect(buttonBox, SIGNAL(accepted()), dlg, SLOT(accept()) );
    connect(buttonBox, SIGNAL(rejected()), dlg, SLOT(reject()) );
573

574
    if (dlg->exec()==QDialog::Accepted&&list->currentRow()>=0)
575
576
    {
        const QString& selectedFile=files[list->currentRow()];
577
        QUrl url = QUrl::fromLocalFile(QDir(dir).absoluteFilePath(selectedFile));
578

579
        qDebug()<<"loading file "<<url;
580
        load(url);
581
    }
582
583

    delete dlg;
584
}
585
586
587
588
589
590
591
592

KParts::ReadWritePart* CantorShell::findPart(QWidget* widget)
{
    foreach( KParts::ReadWritePart* const part, m_parts)
    {
        if(part->widget()==widget)
            return part;
    }
593
    return nullptr;
594
}
595
596
597

void CantorShell::updatePanel()
{
Minh Ngo's avatar
Minh Ngo committed
598
    unplugActionList(QLatin1String("view_show_panel_list"));
599

600
601
602
603
    //remove all of the previous panels (but do not delete the widgets)
    foreach(QDockWidget* dock, m_panels)
    {
        QWidget* widget=dock->widget();
604
        if(widget!=nullptr)
605
606
607
608
609
610
611
612
        {
            widget->setParent(this);
            widget->hide();
        }
        dock->deleteLater();
    }
    m_panels.clear();

613
614
    QList<QAction*> panelActions;

615
    Cantor::PanelPluginHandler* handler=m_part->findChild<Cantor::PanelPluginHandler*>(QLatin1String("PanelPluginHandler"));
616
617
    if(!handler)
    {
618
        qDebug()<<"no PanelPluginHandle found for this part";
619
620
621
        return;
    }

622
    QDockWidget* last=nullptr;
623
624

    QList<Cantor::PanelPlugin*> plugins=handler->plugins();
625
    const bool isNewWorksheet = !m_pluginsVisibility.contains(m_part);
626
627
    foreach(Cantor::PanelPlugin* plugin, plugins)
    {
628
        if(plugin==nullptr)
629
        {
630
            qDebug()<<"somethings wrong";
631
632
633
            continue;
        }

634
        qDebug()<<"adding panel for "<<plugin->name();
635
636
637
638
639
640
641
        plugin->setParentWidget(this);

        QDockWidget* docker=new QDockWidget(plugin->name(), this);
        docker->setObjectName(plugin->name());
        docker->setWidget(plugin->widget());
        addDockWidget ( Qt::RightDockWidgetArea,  docker );

642
        // Set visibility for dock from saved info
643
644
645
        if (isNewWorksheet)
        {
            if (plugin->showOnStartup())
646
647
648
                docker->show();
            else
                docker->hide();
649
        }
650
        else
651
652
653
654
655
656
        {
            if (m_pluginsVisibility[m_part].contains(plugin->name()))
                docker->show();
            else
                docker->hide();
        }
657

658
        if(last!=nullptr)
659
660
661
            tabifyDockWidget(last, docker);
        last=docker;

662
        connect(plugin, &Cantor::PanelPlugin::visibilityRequested, this, &CantorShell::pluginVisibilityRequested);
663

664
        m_panels.append(docker);
665
666
667
668

        //Create the action to show/hide this panel
        panelActions<<docker->toggleViewAction();

669
    }
670

671
    plugActionList(QLatin1String("view_show_panel_list"), panelActions);
672

673
674
675
676
677
    updateNewSubmenu();
}

void CantorShell::updateNewSubmenu()
{
678
    unplugActionList(QLatin1String("new_worksheet_with_backend_list"));
679
680
681
    qDeleteAll(m_newBackendActions);
    m_newBackendActions.clear();

682
683
684
685
    foreach (Cantor::Backend* backend, Cantor::Backend::availableBackends())
    {
        if (!backend->isEnabled())
            continue;
686
        QAction * action = new QAction(QIcon::fromTheme(backend->icon()), backend->name(), nullptr);
687
688
        action->setData(backend->name());
        connect(action, SIGNAL(triggered()), this, SLOT(fileNew()));
689
        m_newBackendActions << action;
690
    }
691
    plugActionList(QLatin1String("new_worksheet_with_backend_list"), m_newBackendActions);
692
}
693
694
695
696
697
698
699
700
701
702

Cantor::WorksheetAccessInterface* CantorShell::currentWorksheetAccessInterface()
{
    Cantor::WorksheetAccessInterface* wa=m_part->findChild<Cantor::WorksheetAccessInterface*>(Cantor::WorksheetAccessInterface::Name);

    if (!wa)
        qDebug()<<"failed to access worksheet access interface for current part";

    return wa;
}
703
704
705
706
707
708
709
710
711
712
713
714
715
716

void CantorShell::pluginVisibilityRequested()
{
    Cantor::PanelPlugin* plugin = static_cast<Cantor::PanelPlugin*>(sender());
    for (QDockWidget* docker: m_panels)
    {
        if (plugin->name() == docker->windowTitle())
        {
            if (docker->isHidden())
                docker->show();
            docker->raise();
        }
    }
}
717
718
719
720
721
722

void CantorShell::onWorksheetSave(const QUrl& url)
{
    if (m_recentProjectsAction)
        m_recentProjectsAction->addUrl(url);
}