cantor.cpp 29.6 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
23
#include <cassert>

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

35
#include <QApplication>
36
#include <QCloseEvent>
37
38
#include <QDebug>
#include <QDockWidget>
39
#include <QDir>
40
#include <QFileDialog>
41
#include <QPushButton>
42
#include <QRegularExpression>
43
#include <QGraphicsView>
44
45

#include "lib/backend.h"
46
47
#include "lib/panelpluginhandler.h"
#include "lib/panelplugin.h"
48
#include "lib/worksheetaccess.h"
49
50
51

#include "settings.h"
#include "ui_settings.h"
52
#include "backendchoosedialog.h"
53
#include <QMetaObject>
54

55
CantorShell::CantorShell() : KParts::MainWindow(), m_part(nullptr), m_panelHandler(nullptr)
56
57
{
    // set the shell's ui resource file
58
    setXMLFile(QLatin1String("cantor_shell.rc"));
59
60
61
62

    // then, setup our actions
    setupActions();

63
    createGUI(nullptr);
64

Filipe Saraiva's avatar
Filipe Saraiva committed
65
    m_tabWidget=new QTabWidget(this);
66
    m_tabWidget->setTabsClosable(true);
67
68
    m_tabWidget->setMovable(true);
    m_tabWidget->setDocumentMode(true);
69
70
71
    setCentralWidget(m_tabWidget);

    connect(m_tabWidget, SIGNAL(currentChanged(int)), this, SLOT(activateWorksheet(int)));
72
    connect(m_tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
73
74
75
76
77

    // 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();
78
79

    setDockOptions(QMainWindow::AnimatedDocks|QMainWindow::AllowTabbedDocks|QMainWindow::VerticalTabs);
80

81
82
    initPanels();

83
    updateNewSubmenu();
84
85
}

86
87
88
89
CantorShell::~CantorShell()
{
    if (m_recentProjectsAction)
        m_recentProjectsAction->saveEntries(KSharedConfig::openConfig()->group(QLatin1String("Recent Files")));
90
91
92
93
94
95
96
97
98
99
100

    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();
101
102
}

103
void CantorShell::load(const QUrl &url)
104
{
105
106
107
108
109
110
111
112
113
    // If the url already opened, then don't open the url in another tab, but
    // just activate the already existed tab
    for (int i = 0; i < m_parts.size(); i++)
    {
        KParts::ReadOnlyPart* part = m_parts[i];
        if (part && part->url() == url)
        {
            if (m_tabWidget->currentIndex() != i)
                activateWorksheet(i);
114
115
            else
                KMessageBox::information(this, i18n("The file %1 is already opened.", QFileInfo(url.toLocalFile()).fileName()), i18n("Open file"));
116
117
118
119
            return;
        }
    }

Alexander Rieder's avatar
Alexander Rieder committed
120
    if (!m_part||!m_part->url().isEmpty() || m_part->isModified() )
121
    {
122
        addWorksheet(QString());
123
124
        m_tabWidget->setCurrentIndex(m_parts.size()-1);
    }
125
126
    if (!m_part->openUrl( url ))
        closeTab(m_tabWidget->currentIndex());
127
128
    if (m_recentProjectsAction)
        m_recentProjectsAction->addUrl(url);
129
130

    updateWindowTitle(m_part->url().fileName());
131
132
}

Alexander Rieder's avatar
Alexander Rieder committed
133
void CantorShell::setupActions()
134
{
135
    QAction* openNew = KStandardAction::openNew(this, SLOT(fileNew()), actionCollection());
136
    openNew->setPriority(QAction::LowPriority);
137
    QAction* open = KStandardAction::open(this, SLOT(fileOpen()), actionCollection());
138
    open->setPriority(QAction::LowPriority);
139
140
141
    m_recentProjectsAction = KStandardAction::openRecent(this, &CantorShell::load, actionCollection());
    m_recentProjectsAction->setPriority(QAction::LowPriority);
    m_recentProjectsAction->loadEntries(KSharedConfig::openConfig()->group(QLatin1String("Recent Files")));
142

143
144
    KStandardAction::close (this,  SLOT(closeTab()),  actionCollection());

145
146
147
148
149
    KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollection());

    createStandardStatusBarAction();
    //setStandardToolBarMenuEnabled(true);

150
151
    KStandardAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
    KStandardAction::configureToolbars(this, SLOT(configureToolbars()), actionCollection());
152
153

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

155
156
157
    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);
158
    connect(downloadExamples, SIGNAL(triggered()), this,  SLOT(downloadExamples()));
159

160
161
162
    QAction * openExample =new QAction(i18n("&Open Example"), actionCollection());
    openExample->setIcon(QIcon::fromTheme(QLatin1String("document-open")));
    actionCollection()->addAction(QLatin1String("file_example_open"), openExample);
163
    connect(openExample, SIGNAL(triggered()), this, SLOT(openExample()));
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

    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);
188
189
}

Alexander Rieder's avatar
Alexander Rieder committed
190
void CantorShell::saveProperties(KConfigGroup & /*config*/)
191
192
193
194
195
196
{
    // 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
197
void CantorShell::readProperties(const KConfigGroup & /*config*/)
198
199
200
201
202
203
204
{
    // 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'
}

205
206
207
208
209
/*!
 * 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
210
void CantorShell::fileNew()
211
{
212
213
214
    QAction* a = static_cast<QAction*>(sender());
    const QString& backendName = a->data().toString();
    if (!backendName.isEmpty())
215
    {
216
217
        addWorksheet(backendName);
        return;
218
    }
219
220

    //"New" action was called -> open the "Choose Backend" dialog.
221
    addWorksheet();
222
223
}

Alexander Rieder's avatar
Alexander Rieder committed
224
void CantorShell::optionsConfigureKeys()
225
{
226
227
    KShortcutsDialog dlg( KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsDisallowed, this );
    dlg.addCollection( actionCollection(), i18n("Cantor") );
228
229
    if (m_part)
        dlg.addCollection( m_part->actionCollection(), i18n("Cantor") );
230
    dlg.configure( true );
231
232
}

Alexander Rieder's avatar
Alexander Rieder committed
233
void CantorShell::fileOpen()
234
235
236
237
{
    // 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
Filipe Saraiva's avatar
Filipe Saraiva committed
238
239
    static const QString& filter = i18n("All supported files (*.cws *ipynb);;Cantor Worksheet (*.cws);;Jupyter Notebook (*.ipynb)");
    QUrl url = QFileDialog::getOpenFileUrl(this, i18n("Open file"), QUrl(), filter, &m_previousFilter);
240
241
242

    if (url.isEmpty() == false)
    {
243
        // About this function, the style guide
244
245
246
247
248
249
250
251
252
253
        // 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
254
            CantorShell* newWin = new CantorShell;
255
256
257
258
259
260
261
            newWin->load( url );
            newWin->show();
            }*/
        load( url );
    }
}

Alexander Rieder's avatar
Alexander Rieder committed
262
void CantorShell::addWorksheet()
263
{
264
265
266
267
268
269
270
271
272
273
274
    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
275
    {
276
277
        QString backend = Settings::self()->defaultBackend();
        if (backend.isEmpty())
278
        {
279
280
281
282
            QPointer<BackendChooseDialog> dlg=new BackendChooseDialog(this);
            if(dlg->exec())
            {
                backend = dlg->backendName();
283
                addWorksheet(backend);
284
285
286
            }

            delete dlg;
287
        }
288
289
290
291
        else
        {
            addWorksheet(backend);
        }
292

293
294
    }else
    {
295
        QTextBrowser *browser=new QTextBrowser(this);
296
        QString backendList=QLatin1String("<ul>");
Andrew Coles's avatar
Andrew Coles committed
297
        int backendListSize = 0;
298
299
        foreach(Cantor::Backend* b, Cantor::Backend::availableBackends())
        {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
300
            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
301
            {
302
                backendList+=QString::fromLatin1("<li>%1: <a href=\"%2\">%2</a></li>").arg(b->name(), b->url());
Andrew Coles's avatar
Andrew Coles committed
303
304
                ++backendListSize;
            }
305
        }
Andrew Coles's avatar
Andrew Coles committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
        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
                              ));
324

325
        browser->setObjectName(QLatin1String("ErrorMessage"));
326
327
        m_tabWidget->addTab(browser, i18n("Error"));
    }
328
329
}

Alexander Rieder's avatar
Alexander Rieder committed
330
void CantorShell::addWorksheet(const QString& backendName)
331
332
333
334
335
336
{
    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
337
338
    KPluginLoader loader(QLatin1String("cantorpart"));
    KPluginFactory* factory = loader.factory();
339
340
    if (factory)
    {
341
        Cantor::Backend* backend = nullptr;
342
        if (!backendName.isEmpty())
343
        {
344
            backend = Cantor::Backend::getBackend(backendName);
345
            if (!backend)
346
            {
347
348
                KMessageBox::error(this, i18n("Backend %1 is not installed", backendName), i18n("Cantor"));
                return;
349
            }
350
            else
351
            {
352
353
354
355
356
                if (!backend->isEnabled())
                {
                    KMessageBox::error(this, i18n("%1 backend installed, but inactive. Please check installation and Cantor settings", backendName), i18n("Cantor"));
                    return;
                }
357
            }
358
        }
359
360
361
362
363
364
365

        // 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)));
366
            connect(part, SIGNAL(showHelp(QString)), this, SIGNAL(showHelp(QString)));
367
            m_parts.append(part);
368
369
370
371
372
            if (backend) // If backend empty (loading worksheet from file), then we connect to signal and wait
                m_parts2Backends[part] = backend->id();
            else
            {
                m_parts2Backends[part] = QString();
373
                connect(part, SIGNAL(setBackendName(QString)), this, SLOT(updateBackendForPart(QString)));
374
            }
375
376
377
378
379
            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();
380
381
382

            // Force run updateCaption for getting proper backend icon
            QMetaObject::invokeMethod(part, "updateCaption");
383
        }
384
        else
385
386
387
        {
            qDebug()<<"error creating part ";
        }
388
389
390
391
392
    }
    else
    {
        // if we couldn't find our Part, we exit since the Shell by
        // itself can't do anything useful
393
        KMessageBox::error(this, i18n("Failed to find the Cantor Part with error %1", loader.errorString()));
394
395
396
397
398
399
400
401
        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
402
void CantorShell::activateWorksheet(int index)
403
{
404
    // Save part panels states before change worksheet
405
406
407
408
409
410
411
412
413
    if (m_part)
    {
        QStringList visiblePanelNames;
        foreach (QDockWidget* doc, m_panels)
        {
            if (doc->widget() && doc->widget()->isVisible())
                visiblePanelNames << doc->objectName();
        }
        m_pluginsVisibility[m_part] = visiblePanelNames;
414
415
416
417
418
419
420
421
422
423

        Cantor::WorksheetAccessInterface* wa=m_part->findChild<Cantor::WorksheetAccessInterface*>(Cantor::WorksheetAccessInterface::Name);
        assert(wa);
        PanelStates states;
        QList<Cantor::PanelPlugin*> plugins=m_panelHandler.plugins(wa->session());
        for(Cantor::PanelPlugin* plugin : plugins)
        {
            states.insert(plugin->name(), plugin->saveState());
        }
        m_pluginsStates[m_part] = states;
424
    }
425

426
    m_part=findPart(m_tabWidget->widget(index));
427
    if(m_part)
428
    {
429
        createGUI(m_part);
430

431
        updateWindowTitle(m_part->url().fileName());
432
433
        updatePanel();
    }
434
    else
435
        qDebug()<<"selected part doesn't exist";
436
437

    m_tabWidget->setCurrentIndex(index);
438
439
}

440
void CantorShell::setTabCaption(const QString& caption, const QIcon& icon)
441
442
{
    KParts::ReadWritePart* part=dynamic_cast<KParts::ReadWritePart*>(sender());
443
444
    if (part)
    {
445
446
        if (!caption.isEmpty())
            m_tabWidget->setTabText(m_tabWidget->indexOf(part->widget()), caption);
447
448
        m_tabWidget->setTabIcon(m_tabWidget->indexOf(part->widget()), icon);
    }
449
450
}

451
452
453
454
455
456
void CantorShell::updateWindowTitle(const QString& fileName)
{
    QFileInfo info(fileName);
    setWindowTitle(info.baseName());
}

457
void CantorShell::closeTab(int index)
458
{
459
    if (index != -1)
460
    {
461
462
463
464
465
466
467
468
469
470
471
472
        QWidget* widget = m_tabWidget->widget(index);
        if (widget)
        {
            KParts::ReadWritePart* part= findPart(widget);
            if (part && !reallyCloseThisPart(part))
                return;
        }
    }
    else
    {
        if (!reallyClose(false))
            return;
473
    }
474
475
476

    QWidget* widget = nullptr;
    if (index >= 0)
477
    {
478
479
480
481
482
483
484
485
486
487
488
        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;
489
    }
490

491

492
493
    m_tabWidget->removeTab(index);

494
    bool isCurrectPartClosed = m_part ? widget == m_part->widget() : false;
495
    if(widget->objectName()==QLatin1String("ErrorMessage"))
496
497
498
499
500
501
502
    {
        widget->deleteLater();
    }else
    {
        KParts::ReadWritePart* part= findPart(widget);
        if(part)
        {
503
504
            saveDockPanelsState(part);

505
            m_parts.removeAll(part);
506
            m_pluginsVisibility.remove(part);
507
            m_parts2Backends.remove(part);
508
            m_pluginsStates.remove(part);
509
            delete part;
510
511
        }
    }
512
513
514

    if (m_tabWidget->count() == 0)
        setCaption(QString());
515
516
517

    if (isCurrectPartClosed || m_part == nullptr)
        updatePanel();
518
519
}

520
521
522
bool CantorShell::reallyClose(bool checkAllParts) {
    if(checkAllParts && m_parts.count() > 1) {
        bool modified = false;
523
        foreach( KParts::ReadWritePart* const part, m_parts)
524
525
526
527
528
529
530
531
        {
            if(part->isModified()) {
                modified = true;
                break;
            }
        }
        if(!modified) return true;
        int want_save = KMessageBox::warningYesNo( this,
532
            i18n("Multiple unsaved Worksheets are opened. Do you want to close them?"),
533
534
535
536
537
538
539
540
            i18n("Close Cantor"));
        switch (want_save) {
            case KMessageBox::Yes:
                return true;
            case KMessageBox::No:
                return false;
        }
    }
541
542
543
544
545
546
547

    return reallyCloseThisPart(m_part);
}

bool CantorShell::reallyCloseThisPart(KParts::ReadWritePart* part)
{
     if (part && part->isModified() ) {
548
549
550
551
552
        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:
553
554
                part->save();
                if(part->waitSaveComplete()) {
555
556
                    return true;
                } else {
557
                    part->setModified(true);
558
559
560
561
562
563
564
565
566
567
568
                    return false;
                }
            case KMessageBox::Cancel:
                return false;
            case KMessageBox::No:
                return true;
        }
    }
    return true;
}

569

570
571
572
573
void CantorShell::closeEvent(QCloseEvent* event) {
    if(!reallyClose()) {
        event->ignore();
    } else {
574
575
576
        for (KParts::ReadWritePart* part : m_parts)
            saveDockPanelsState(part);

577
578
579
580
        KParts::MainWindow::closeEvent(event);
    }
}

Alexander Rieder's avatar
Alexander Rieder committed
581
void CantorShell::showSettings()
582
{
583
    KConfigDialog *dialog = new KConfigDialog(this,  QLatin1String("settings"), Settings::self());
584
585
586
    QWidget *generalSettings = new QWidget;
    Ui::SettingsBase base;
    base.setupUi(generalSettings);
Alexander Rieder's avatar
Alexander Rieder committed
587
    base.kcfg_DefaultBackend->addItems(Cantor::Backend::listAvailableBackends());
588

589
    dialog->addPage(generalSettings, i18n("General"), QLatin1String("preferences-other"));
Alexander Rieder's avatar
Alexander Rieder committed
590
    foreach(Cantor::Backend* backend, Cantor::Backend::availableBackends())
591
592
593
594
595
596
597
    {
        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();
}
598

Alexander Rieder's avatar
Alexander Rieder committed
599
void CantorShell::downloadExamples()
600
{
601
602
603
    KNS3::DownloadDialog dialog;
    dialog.exec();
    foreach (const KNS3::Entry& e,  dialog.changedEntries())
604
    {
605
        qDebug() << "Changed Entry: " << e.name();
606
607
    }
}
608

Alexander Rieder's avatar
Alexander Rieder committed
609
void CantorShell::openExample()
610
{
611
    QString dir = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1String("/examples");
612
    if (dir.isEmpty()) return;
613
    QDir().mkpath(dir);
614
615

    QStringList files=QDir(dir).entryList(QDir::Files);
616
    QPointer<QDialog> dlg=new QDialog(this);
617
    QListWidget* list=new QListWidget(dlg);
618
619
620
    foreach(const QString& file, files)
    {
        QString name=file;
621
        name.remove(QRegularExpression(QStringLiteral("-.*\\.hotstuff-access$")));
622
623
624
        list->addItem(name);
    }

625
626
627
628
629
630
631
632
633
634
635
636
637
    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()) );
638

639
    if (dlg->exec()==QDialog::Accepted&&list->currentRow()>=0)
640
641
    {
        const QString& selectedFile=files[list->currentRow()];
642
        QUrl url = QUrl::fromLocalFile(QDir(dir).absoluteFilePath(selectedFile));
643

644
        qDebug()<<"loading file "<<url;
645
        load(url);
646
    }
647
648

    delete dlg;
649
}
650
651
652
653
654
655
656
657

KParts::ReadWritePart* CantorShell::findPart(QWidget* widget)
{
    foreach( KParts::ReadWritePart* const part, m_parts)
    {
        if(part->widget()==widget)
            return part;
    }
658
    return nullptr;
659
}
660

661
void CantorShell::initPanels()
662
{
663
    m_panelHandler.loadPlugins();
664

665
666
    QList<Cantor::PanelPlugin*> plugins = m_panelHandler.allPlugins();
    foreach(Cantor::PanelPlugin* plugin, plugins)
667
    {
668
        if(plugin==nullptr)
669
        {
670
671
            qDebug()<<"somethings wrong";
            continue;
672
673
        }

674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
        qDebug()<<"adding panel for "<<plugin->name();
        plugin->setParentWidget(this);
        plugin->connectToShell(this);

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

        docker->hide();

        connect(plugin, &Cantor::PanelPlugin::visibilityRequested, this, &CantorShell::pluginVisibilityRequested);
        connect(plugin, &Cantor::PanelPlugin::requestRunCommand, this, &CantorShell::pluginCommandRunRequested);

        m_panels.append(docker);
689

690
    }
691
}
692

693

694
695
696
697
698
699
700
void CantorShell::updatePanel()
{
    unplugActionList(QLatin1String("view_show_panel_list"));

    QList<QAction*> panelActions;

    bool isNewWorksheet = !m_pluginsVisibility.contains(m_part);
701
702
703
704
705
706
707
708
709
710
    if (isNewWorksheet)
    {
        KConfigGroup panelStatusGroup(KSharedConfig::openConfig(), QLatin1String("PanelsStatus"));
        if (m_parts2Backends.contains(m_part) && panelStatusGroup.hasKey(m_parts2Backends[m_part]))
        {
            const QStringList& plugins = panelStatusGroup.readEntry(m_parts2Backends[m_part]).split(QLatin1Char('\n'));
            m_pluginsVisibility[m_part] = plugins;
            isNewWorksheet = false;
        }
    }
711

712
713
714
715
716
717
718
    Cantor::WorksheetAccessInterface* wa = nullptr;
    if (m_part)
        wa = m_part->findChild<Cantor::WorksheetAccessInterface*>(Cantor::WorksheetAccessInterface::Name);

    // Worksheet interface can be missing on m_part clossing (and m_part on this moment can be nullptr)
    QList<Cantor::PanelPlugin*> plugins;
    if (wa)
719
    {
720
721
722
        QDockWidget* last=nullptr;
        plugins = m_panelHandler.plugins(wa->session());
        for(Cantor::PanelPlugin* plugin : plugins)
723
        {
724
725
726
727
728
            if(plugin==nullptr)
            {
                qDebug()<<"somethings wrong";
                continue;
            }
729

730
            qDebug()<<"adding panel for "<<plugin->name();
731

732
733
            if (m_pluginsStates.contains(m_part))
                plugin->restoreState(m_pluginsStates[m_part][plugin->name()]);
734
            else
735
736
737
738
739
            {
                Cantor::PanelPlugin::State initState;
                initState.session = wa->session();
                plugin->restoreState(initState);
            }
740

741
742
743
744
745
746
747
            QDockWidget* foundDocker = nullptr;
            for (QDockWidget* docker : m_panels)
                if (docker->objectName() == plugin->name())
                {
                    foundDocker = docker;
                    break;
                }
748

749
750
751
752
753
            if (!foundDocker)
            {
                qDebug() << "something wrong: can't find panel for plugin \"" << plugin->name() << "\"";
                continue;
            }
754

755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
            // Set visibility for dock from saved info
            if (isNewWorksheet)
            {
                if (plugin->showOnStartup())
                    foundDocker->show();
                else
                    foundDocker->hide();
            }
            else
            {
                if (m_pluginsVisibility[m_part].contains(plugin->name()))
                    foundDocker->show();
                else
                    foundDocker->hide();
            }

            if(last!=nullptr)
                tabifyDockWidget(last, foundDocker);
            last = foundDocker;
774

775
776
            //Create the action to show/hide this panel
            panelActions<<foundDocker->toggleViewAction();
777

778
779
780
781
782
783
784
785
786
787
788
789
790
791
        }
    }

    // Hide plugins, which don't supported on current session
    QList<Cantor::PanelPlugin*> allPlugins=m_panelHandler.allPlugins();
    for(Cantor::PanelPlugin* plugin : allPlugins)
    {
        if (plugins.indexOf(plugin) == -1)
            for (QDockWidget* docker : m_panels)
                if (docker->objectName() == plugin->name())
                {
                    docker->hide();
                    break;
                }
792
    }
793

794
    plugActionList(QLatin1String("view_show_panel_list"), panelActions);
795

796
797
798
799
800
    updateNewSubmenu();
}

void CantorShell::updateNewSubmenu()
{
801
    unplugActionList(QLatin1String("new_worksheet_with_backend_list"));
802
803
804
    qDeleteAll(m_newBackendActions);
    m_newBackendActions.clear();

805
806
807
808
    foreach (Cantor::Backend* backend, Cantor::Backend::availableBackends())
    {
        if (!backend->isEnabled())
            continue;
809
        QAction * action = new QAction(QIcon::fromTheme(backend->icon()), backend->name(), nullptr);
810
811
        action->setData(backend->name());
        connect(action, SIGNAL(triggered()), this, SLOT(fileNew()));
812
        m_newBackendActions << action;
813
    }
814
    plugActionList(QLatin1String("new_worksheet_with_backend_list"), m_newBackendActions);
815
}
816
817
818
819
820
821
822
823
824
825

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;
}
826
827
828
829
830
831

void CantorShell::pluginVisibilityRequested()
{
    Cantor::PanelPlugin* plugin = static_cast<Cantor::PanelPlugin*>(sender());
    for (QDockWidget* docker: m_panels)
    {
832
        if (plugin->name() == docker->objectName())
833
834
835
836
837
838
839
        {
            if (docker->isHidden())
                docker->show();
            docker->raise();
        }
    }
}
840
841
842
843
844

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

    updateWindowTitle(m_part->url().fileName());
847
}
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869

void CantorShell::saveDockPanelsState(KParts::ReadWritePart* part)
{
    if (m_parts2Backends.contains(part))
    {

        QStringList visiblePanelNames;
        if (part == m_part)
        {
            foreach (QDockWidget* doc, m_panels)
            {
                if (doc->widget() && doc->widget()->isVisible())
                    visiblePanelNames << doc->objectName();
            }
        }
        else if (m_pluginsVisibility.contains(part))
            visiblePanelNames = m_pluginsVisibility[part];

        KConfigGroup panelStatusGroup(KSharedConfig::openConfig(), QLatin1String("PanelsStatus"));
        panelStatusGroup.writeEntry(m_parts2Backends[part], visiblePanelNames.join(QLatin1Char('\n')));
    }
}
870
871
872
873
874

void CantorShell::updateBackendForPart(const QString& backend)
{
    KParts::ReadWritePart* part=dynamic_cast<KParts::ReadWritePart*>(sender());
    if (part && m_parts2Backends.contains(part) && m_parts2Backends[part].isEmpty())
875
    {
876
        m_parts2Backends[part] = backend;
877
878
879
880
881
882
883
884
885
886
887
888
889
890

        KConfigGroup panelStatusGroup(KSharedConfig::openConfig(), QLatin1String("PanelsStatus"));
        if (m_part == part && panelStatusGroup.hasKey(backend))
        {
            const QStringList& plugins = panelStatusGroup.readEntry(m_parts2Backends[m_part]).split(QLatin1Char('\n'));
            m_pluginsVisibility[m_part] = plugins;

            for (QDockWidget* docker : m_panels)
                if (m_pluginsVisibility[m_part].contains(docker->objectName()))
                    docker->show();
                else
                    docker->hide();
        }
    }
891
}
892
893
894
895
896
897
898
899

void CantorShell::pluginCommandRunRequested(const QString& cmd)
{
    if (m_part)
    {
        QMetaObject::invokeMethod(m_part, "runCommand", Qt::QueuedConnection, Q_ARG(QString, cmd));
    }
}