cantor.cpp 26.3 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
 */
20

21
#include <KActionCollection>
22
23
#include <KConfigDialog>
#include <KConfigGroup>
24
#include <KConfig>
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
#include <QDebug>
35
#include <QDir>
36
#include <QDockWidget>
37
#include <QFileDialog>
38
#include <QGraphicsView>
39
#include <QPushButton>
40
#include <QRegularExpression>
41
42

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

47
48
#include "backendchoosedialog.h"
#include "cantor.h"
49
50
51
#include "settings.h"
#include "ui_settings.h"

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

    // then, setup our actions
    setupActions();

60
    createGUI(nullptr);
61

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

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

    // 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();
75
76

    setDockOptions(QMainWindow::AnimatedDocks|QMainWindow::AllowTabbedDocks|QMainWindow::VerticalTabs);
77
78

    updateNewSubmenu();
79
80
}

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

    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();
96
97
}

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

    updateWindowTitle(m_part->url().fileName());
111
112
}

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

123
124
    KStandardAction::close (this,  SLOT(closeTab()),  actionCollection());

125
126
127
128
129
    KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollection());

    createStandardStatusBarAction();
    //setStandardToolBarMenuEnabled(true);

130
131
    KStandardAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
    KStandardAction::configureToolbars(this, SLOT(configureToolbars()), actionCollection());
132
133

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

135
136
137
    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);
138
    connect(downloadExamples, SIGNAL(triggered()), this,  SLOT(downloadExamples()));
139

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

    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);
168
169
}

Alexander Rieder's avatar
Alexander Rieder committed
170
void CantorShell::saveProperties(KConfigGroup & /*config*/)
171
172
173
174
175
176
{
    // 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
177
void CantorShell::readProperties(const KConfigGroup & /*config*/)
178
179
180
181
182
183
184
{
    // 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'
}

185
186
187
188
189
/*!
 * 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
190
void CantorShell::fileNew()
191
{
192
193
194
    QAction* a = static_cast<QAction*>(sender());
    const QString& backendName = a->data().toString();
    if (!backendName.isEmpty())
195
    {
196
197
        addWorksheet(backendName);
        return;
198
    }
199
200

    //"New" action was called -> open the "Choose Backend" dialog.
201
    addWorksheet();
202
203
}

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

Alexander Rieder's avatar
Alexander Rieder committed
213
void CantorShell::fileOpen()
214
215
216
217
{
    // 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
218
219
    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);
220
221
222

    if (url.isEmpty() == false)
    {
223
        // About this function, the style guide
224
225
226
227
228
229
230
231
232
233
        // 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
234
            CantorShell* newWin = new CantorShell;
235
236
237
238
239
240
241
            newWin->load( url );
            newWin->show();
            }*/
        load( url );
    }
}

Alexander Rieder's avatar
Alexander Rieder committed
242
void CantorShell::addWorksheet()
243
{
244
245
246
247
248
249
250
251
252
253
254
    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
255
    {
256
257
        QString backend = Settings::self()->defaultBackend();
        if (backend.isEmpty())
258
        {
259
260
261
262
            QPointer<BackendChooseDialog> dlg=new BackendChooseDialog(this);
            if(dlg->exec())
            {
                backend = dlg->backendName();
263
                addWorksheet(backend);
264
265
266
            }

            delete dlg;
267
        }
268
269
270
271
        else
        {
            addWorksheet(backend);
        }
272

273
274
    }else
    {
275
        QTextBrowser *browser=new QTextBrowser(this);
276
        QString backendList=QLatin1String("<ul>");
Andrew Coles's avatar
Andrew Coles committed
277
        int backendListSize = 0;
278
279
        foreach(Cantor::Backend* b, Cantor::Backend::availableBackends())
        {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
280
            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
281
            {
282
                backendList+=QString::fromLatin1("<li>%1: <a href=\"%2\">%2</a></li>").arg(b->name(), b->url());
Andrew Coles's avatar
Andrew Coles committed
283
284
                ++backendListSize;
            }
285
        }
Andrew Coles's avatar
Andrew Coles committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
        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
                              ));
304

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

Alexander Rieder's avatar
Alexander Rieder committed
310
void CantorShell::addWorksheet(const QString& backendName)
311
312
313
314
315
316
{
    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
317
318
    KPluginLoader loader(QLatin1String("cantorpart"));
    KPluginFactory* factory = loader.factory();
319
320
    if (factory)
    {
321
        Cantor::Backend* backend = nullptr;
322
        if (!backendName.isEmpty())
323
        {
324
            backend = Cantor::Backend::getBackend(backendName);
325
            if (!backend)
326
            {
327
328
                KMessageBox::error(this, i18n("Backend %1 is not installed", backendName), i18n("Cantor"));
                return;
329
            }
330
            else
331
            {
332
333
334
335
336
                if (!backend->isEnabled())
                {
                    KMessageBox::error(this, i18n("%1 backend installed, but inactive. Please check installation and Cantor settings", backendName), i18n("Cantor"));
                    return;
                }
337
            }
338
        }
339
340
341
342
343
344
345

        // 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)));
346
            connect(part, SIGNAL(requestOpenWorksheet(QUrl)), this, SLOT(load(QUrl)));
347
            m_parts.append(part);
348
349
350
351
352
353
354
            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();
                connect(part, SIGNAL(setBackendName(QString)), this, SLOT(updateBackendForPart(setBackendName)));
            }
355
356
357
358
359
            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();
360
361
362

            // Force run updateCaption for getting proper backend icon
            QMetaObject::invokeMethod(part, "updateCaption");
363
        }
364
        else
365
366
367
        {
            qDebug()<<"error creating part ";
        }
368
369
370
371
372
    }
    else
    {
        // if we couldn't find our Part, we exit since the Shell by
        // itself can't do anything useful
373
        KMessageBox::error(this, i18n("Failed to find the Cantor Part with error %1", loader.errorString()));
374
375
376
377
378
379
380
381
        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
382
void CantorShell::activateWorksheet(int index)
383
{
384
    QObject* pluginHandler=m_part->findChild<QObject*>(QLatin1String("PanelPluginHandler"));
385
386
387
388
389
390
391
392
393
394
395
396
397
398
    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;
    }
399

400
    m_part=findPart(m_tabWidget->widget(index));
401
    if(m_part)
402
    {
403
        createGUI(m_part);
404

405
        updateWindowTitle(m_part->url().fileName());
406
        QObject* pluginHandler=m_part->findChild<QObject*>(QLatin1String("PanelPluginHandler"));
407
408
409
        connect(pluginHandler, SIGNAL(pluginsChanged()), this, SLOT(updatePanel()));
        updatePanel();
    }
410
    else
411
        qDebug()<<"selected part doesn't exist";
412
413

    m_tabWidget->setCurrentIndex(index);
414
415
}

416
void CantorShell::setTabCaption(const QString& caption, const QIcon& icon)
417
418
{
    KParts::ReadWritePart* part=dynamic_cast<KParts::ReadWritePart*>(sender());
419
420
    if (part)
    {
421
422
        if (!caption.isEmpty())
            m_tabWidget->setTabText(m_tabWidget->indexOf(part->widget()), caption);
423
424
        m_tabWidget->setTabIcon(m_tabWidget->indexOf(part->widget()), icon);
    }
425
426
}

427
428
429
430
431
432
void CantorShell::updateWindowTitle(const QString& fileName)
{
    QFileInfo info(fileName);
    setWindowTitle(info.baseName());
}

433
void CantorShell::closeTab(int index)
434
{
435
    if (index != -1)
436
    {
437
438
439
440
441
442
443
444
445
446
447
448
        QWidget* widget = m_tabWidget->widget(index);
        if (widget)
        {
            KParts::ReadWritePart* part= findPart(widget);
            if (part && !reallyCloseThisPart(part))
                return;
        }
    }
    else
    {
        if (!reallyClose(false))
            return;
449
    }
450
451
452

    QWidget* widget = nullptr;
    if (index >= 0)
453
    {
454
455
456
457
458
459
460
461
462
463
464
        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;
465
    }
466

467

468
469
    m_tabWidget->removeTab(index);

470
    if(widget->objectName()==QLatin1String("ErrorMessage"))
471
472
473
474
475
476
477
    {
        widget->deleteLater();
    }else
    {
        KParts::ReadWritePart* part= findPart(widget);
        if(part)
        {
478
479
            saveDockPanelsState(part);

480
            m_parts.removeAll(part);
481
            m_pluginsVisibility.remove(part);
482
            m_parts2Backends.remove(part);
483
            delete part;
484
485
        }
    }
486
487
488

    if (m_tabWidget->count() == 0)
        setCaption(QString());
489
    updatePanel();
490
491
}

492
493
494
bool CantorShell::reallyClose(bool checkAllParts) {
    if(checkAllParts && m_parts.count() > 1) {
        bool modified = false;
495
        foreach( KParts::ReadWritePart* const part, m_parts)
496
497
498
499
500
501
502
503
        {
            if(part->isModified()) {
                modified = true;
                break;
            }
        }
        if(!modified) return true;
        int want_save = KMessageBox::warningYesNo( this,
504
            i18n("Multiple unsaved Worksheets are opened. Do you want to close them?"),
505
506
507
508
509
510
511
512
            i18n("Close Cantor"));
        switch (want_save) {
            case KMessageBox::Yes:
                return true;
            case KMessageBox::No:
                return false;
        }
    }
513
514
515
516
517
518
519

    return reallyCloseThisPart(m_part);
}

bool CantorShell::reallyCloseThisPart(KParts::ReadWritePart* part)
{
     if (part && part->isModified() ) {
520
521
522
523
524
        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:
525
526
                part->save();
                if(part->waitSaveComplete()) {
527
528
                    return true;
                } else {
529
                    part->setModified(true);
530
531
532
533
534
535
536
537
538
539
540
                    return false;
                }
            case KMessageBox::Cancel:
                return false;
            case KMessageBox::No:
                return true;
        }
    }
    return true;
}

541

542
543
544
545
void CantorShell::closeEvent(QCloseEvent* event) {
    if(!reallyClose()) {
        event->ignore();
    } else {
546
547
548
        for (KParts::ReadWritePart* part : m_parts)
            saveDockPanelsState(part);

549
550
551
552
        KParts::MainWindow::closeEvent(event);
    }
}

Alexander Rieder's avatar
Alexander Rieder committed
553
void CantorShell::showSettings()
554
{
555
    KConfigDialog *dialog = new KConfigDialog(this,  QLatin1String("settings"), Settings::self());
556
557
558
    QWidget *generalSettings = new QWidget;
    Ui::SettingsBase base;
    base.setupUi(generalSettings);
Alexander Rieder's avatar
Alexander Rieder committed
559
    base.kcfg_DefaultBackend->addItems(Cantor::Backend::listAvailableBackends());
560

561
    dialog->addPage(generalSettings, i18n("General"), QLatin1String("preferences-other"));
Alexander Rieder's avatar
Alexander Rieder committed
562
    foreach(Cantor::Backend* backend, Cantor::Backend::availableBackends())
563
564
565
566
567
568
569
    {
        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();
}
570

Alexander Rieder's avatar
Alexander Rieder committed
571
void CantorShell::downloadExamples()
572
{
573
574
575
    KNS3::DownloadDialog dialog;
    dialog.exec();
    foreach (const KNS3::Entry& e,  dialog.changedEntries())
576
    {
577
        qDebug() << "Changed Entry: " << e.name();
578
579
    }
}
580

Alexander Rieder's avatar
Alexander Rieder committed
581
void CantorShell::openExample()
582
{
583
    QString dir = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1String("/examples");
584
    if (dir.isEmpty()) return;
585
    QDir().mkpath(dir);
586
587

    QStringList files=QDir(dir).entryList(QDir::Files);
588
    QPointer<QDialog> dlg=new QDialog(this);
589
    QListWidget* list=new QListWidget(dlg);
590
591
592
    foreach(const QString& file, files)
    {
        QString name=file;
593
        name.remove(QRegularExpression(QStringLiteral("-.*\\.hotstuff-access$")));
594
595
596
        list->addItem(name);
    }

597
598
599
600
601
602
603
604
605
606
607
608
609
    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()) );
610

611
    if (dlg->exec()==QDialog::Accepted&&list->currentRow()>=0)
612
613
    {
        const QString& selectedFile=files[list->currentRow()];
614
        QUrl url = QUrl::fromLocalFile(QDir(dir).absoluteFilePath(selectedFile));
615

616
        qDebug()<<"loading file "<<url;
617
        load(url);
618
    }
619
620

    delete dlg;
621
}
622
623
624
625
626
627
628
629

KParts::ReadWritePart* CantorShell::findPart(QWidget* widget)
{
    foreach( KParts::ReadWritePart* const part, m_parts)
    {
        if(part->widget()==widget)
            return part;
    }
630
    return nullptr;
631
}
632
633
634

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

637
638
639
640
    //remove all of the previous panels (but do not delete the widgets)
    foreach(QDockWidget* dock, m_panels)
    {
        QWidget* widget=dock->widget();
641
        if(widget!=nullptr)
642
643
644
645
646
647
648
649
        {
            widget->setParent(this);
            widget->hide();
        }
        dock->deleteLater();
    }
    m_panels.clear();

650
651
    QList<QAction*> panelActions;

652
    Cantor::PanelPluginHandler* handler=m_part->findChild<Cantor::PanelPluginHandler*>(QLatin1String("PanelPluginHandler"));
653
654
    if(!handler)
    {
655
        qDebug()<<"no PanelPluginHandle found for this part";
656
657
658
        return;
    }

659
    QDockWidget* last=nullptr;
660
661
662
663
664
665
666
667
668
669
670
671
    bool isNewWorksheet = !m_pluginsVisibility.contains(m_part);

    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;
        }
    }
672
673
674
675

    QList<Cantor::PanelPlugin*> plugins=handler->plugins();
    foreach(Cantor::PanelPlugin* plugin, plugins)
    {
676
        if(plugin==nullptr)
677
        {
678
            qDebug()<<"somethings wrong";
679
680
681
            continue;
        }

682
        qDebug()<<"adding panel for "<<plugin->name();
683
684
685
686
687
688
689
        plugin->setParentWidget(this);

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

690
        // Set visibility for dock from saved info
691
692
693
        if (isNewWorksheet)
        {
            if (plugin->showOnStartup())
694
695
696
                docker->show();
            else
                docker->hide();
697
        }
698
        else
699
700
701
702
703
704
        {
            if (m_pluginsVisibility[m_part].contains(plugin->name()))
                docker->show();
            else
                docker->hide();
        }
705

706
        if(last!=nullptr)
707
708
709
            tabifyDockWidget(last, docker);
        last=docker;

710
        connect(plugin, &Cantor::PanelPlugin::visibilityRequested, this, &CantorShell::pluginVisibilityRequested);
711

712
        m_panels.append(docker);
713
714
715
716

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

717
    }
718

719
    plugActionList(QLatin1String("view_show_panel_list"), panelActions);
720

721
722
723
724
725
    updateNewSubmenu();
}

void CantorShell::updateNewSubmenu()
{
726
    unplugActionList(QLatin1String("new_worksheet_with_backend_list"));
727
728
729
    qDeleteAll(m_newBackendActions);
    m_newBackendActions.clear();

730
731
732
733
    foreach (Cantor::Backend* backend, Cantor::Backend::availableBackends())
    {
        if (!backend->isEnabled())
            continue;
734
        QAction * action = new QAction(QIcon::fromTheme(backend->icon()), backend->name(), nullptr);
735
736
        action->setData(backend->name());
        connect(action, SIGNAL(triggered()), this, SLOT(fileNew()));
737
        m_newBackendActions << action;
738
    }
739
    plugActionList(QLatin1String("new_worksheet_with_backend_list"), m_newBackendActions);
740
}
741
742
743
744
745
746
747
748
749
750

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;
}
751
752
753
754
755
756
757
758
759
760
761
762
763
764

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();
        }
    }
}
765
766
767
768
769

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

    updateWindowTitle(m_part->url().fileName());
772
}
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794

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')));
    }
}
795
796
797
798
799
800
801

void CantorShell::updateBackendForPart(const QString& backend)
{
    KParts::ReadWritePart* part=dynamic_cast<KParts::ReadWritePart*>(sender());
    if (part && m_parts2Backends.contains(part) && m_parts2Backends[part].isEmpty())
        m_parts2Backends[part] = backend;
}