kdiff3_shell.cpp 5.76 KB
Newer Older
Joachim Eibl's avatar
Joachim Eibl committed
1
/***************************************************************************
Joachim Eibl's avatar
Joachim Eibl committed
2
 * Copyright (C) 2003-2007 Joachim Eibl <joachim.eibl at gmx.de>           *
Joachim Eibl's avatar
Joachim Eibl committed
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 Steet, Fifth Floor, Boston, MA 02110-1301, USA.           *
 ***************************************************************************/
Joachim Eibl's avatar
Joachim Eibl committed
19 20 21

#include "kdiff3_shell.h"
#include "kdiff3.h"
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
22
#include "kdiff3_part.h"
Joachim Eibl's avatar
Joachim Eibl committed
23

24 25 26
#include <KPluginMetaData>
#include <KPluginLoader>

Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
27
#include <kshortcutsdialog.h>
Joachim Eibl's avatar
Joachim Eibl committed
28 29
#include <kfiledialog.h>
#include <kconfig.h>
30
#include <QUrl>
Joachim Eibl's avatar
Joachim Eibl committed
31 32 33

#include <kedittoolbar.h>

Michael Reeves's avatar
Michael Reeves committed
34
#include <QAction>
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
35
#include <kstandardaction.h>
Joachim Eibl's avatar
Joachim Eibl committed
36 37 38

#include <klibloader.h>
#include <kmessagebox.h>
39
#include <QStatusBar>
Joachim Eibl's avatar
Joachim Eibl committed
40 41
#include <klocale.h>

Joachim Eibl's avatar
Joachim Eibl committed
42
#include <QStatusBar>
Joachim Eibl's avatar
Joachim Eibl committed
43
#include <QCloseEvent>
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
44
#include <ktoolbar.h>
Joachim Eibl's avatar
Joachim Eibl committed
45

Joachim Eibl's avatar
Joachim Eibl committed
46

Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
47 48
KDiff3Shell::KDiff3Shell( bool bCompleteInit )
    : KParts::MainWindow( )
Joachim Eibl's avatar
Joachim Eibl committed
49 50 51 52 53 54 55 56
{
    m_bUnderConstruction = true;
    // set the shell's ui resource file
    setXMLFile("kdiff3_shell.rc");

    // and a status bar
    statusBar()->show();

57 58 59 60 61 62 63 64 65
    const QVector<KPluginMetaData> plugin_offers = KPluginLoader::findPlugins("kf5/kdiff3part");
    foreach (const KPluginMetaData &service, plugin_offers) {
	    KPluginFactory *factory = KPluginLoader(service.fileName()).factory();
	    m_part = factory->create<KDiff3Part>(this, QVariantList() << QVariant(QLatin1String("KDiff3Part")));
	    if (m_part)
	      break;
    }
    
    //m_part = new KDiff3Part( this, this, QVariantList() << QVariant(QLatin1String("KDiff3Part")) );
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79

   if (m_part)
   {
      // and integrate the part's GUI with the shell's
      createGUI(m_part);
      //toolBar()->setToolButtonStyle( Qt::ToolButtonIconOnly );

      // tell the KParts::MainWindow that this is indeed the main widget
      setCentralWidget(m_part->widget());

      if (bCompleteInit)
	 ((KDiff3App*)m_part->widget())->completeInit();
      connect(((KDiff3App*)m_part->widget()), SIGNAL(createNewInstance(const QString&, const QString&, const QString&)), this, SLOT(slotNewInstance(const QString&, const QString&, const QString&)));
   }
Joachim Eibl's avatar
Joachim Eibl committed
80 81 82 83
    else
    {
        // if we couldn't find our Part, we exit since the Shell by
        // itself can't do anything useful
Joachim Eibl's avatar
Joachim Eibl committed
84
        KMessageBox::error(this, i18n("Could not initialize the KDiff3 part.\n"
Joachim Eibl's avatar
Joachim Eibl committed
85 86 87
           "This usually happens due to an installation problem. "
           "Please read the README-file in the source package for details.")
           );
Joachim Eibl's avatar
0.9.86  
Joachim Eibl committed
88
        //kapp->quit();
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
89

Joachim Eibl's avatar
0.9.86  
Joachim Eibl committed
90 91
        ::exit(-1); //kapp->quit() doesn't work here yet.

Joachim Eibl's avatar
Joachim Eibl committed
92 93
        // we return here, cause kapp->quit() only means "exit the
        // next time we enter the event loop...
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
94

Joachim Eibl's avatar
Joachim Eibl committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        return;
    }

    // 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();
    m_bUnderConstruction = false;
}

KDiff3Shell::~KDiff3Shell()
{
}

bool KDiff3Shell::queryClose()
{
   if (m_part)
      return ((KDiff3App*)m_part->widget())->queryClose();
   else
      return true;
}

bool KDiff3Shell::queryExit()
{
   return true;
}

Joachim Eibl's avatar
Joachim Eibl committed
122 123 124 125 126 127
void KDiff3Shell::closeEvent(QCloseEvent*e)
{
   if ( queryClose() )
   {
      e->accept();
      bool bFileSaved = ((KDiff3App*)m_part->widget())->isFileSaved();
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
128
      bool bDirCompare = ((KDiff3App*)m_part->widget())->isDirComparison();
129
      QApplication::exit( bFileSaved || bDirCompare ? 0 : 1 );
Joachim Eibl's avatar
Joachim Eibl committed
130 131 132 133
   }
   else
      e->ignore();
}
Joachim Eibl's avatar
Joachim Eibl committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

void KDiff3Shell::optionsShowToolbar()
{
    // this is all very cut and paste code for showing/hiding the
    // toolbar
    if (m_toolbarAction->isChecked())
        toolBar()->show();
    else
        toolBar()->hide();
}

void KDiff3Shell::optionsShowStatusbar()
{
    // this is all very cut and paste code for showing/hiding the
    // statusbar
    if (m_statusbarAction->isChecked())
        statusBar()->show();
    else
        statusBar()->hide();
}

void KDiff3Shell::optionsConfigureKeys()
{
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
157
    KShortcutsDialog::configure(actionCollection() /*, "kdiff3_shell.rc" */ );
Joachim Eibl's avatar
Joachim Eibl committed
158 159 160 161
}

void KDiff3Shell::optionsConfigureToolbars()
{
162
   KConfigGroup mainWindowGroup(  KSharedConfig::openConfig(), "MainWindow" );
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
163
   saveMainWindowSettings( mainWindowGroup );
Joachim Eibl's avatar
Joachim Eibl committed
164 165

    // use the standard toolbar editor
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
166
    KEditToolBar dlg(factory());
167
    connect(&dlg, &KEditToolBar::newToolbarConfig, this, &KDiff3Shell::applyNewToolbarConfig);
Joachim Eibl's avatar
Joachim Eibl committed
168 169 170 171 172
    dlg.exec();
}

void KDiff3Shell::applyNewToolbarConfig()
{
173
   KConfigGroup mainWindowGroup(  KSharedConfig::openConfig(), "MainWindow" );
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
174
   applyMainWindowSettings( mainWindowGroup );
Joachim Eibl's avatar
Joachim Eibl committed
175 176
}

Joachim Eibl's avatar
Joachim Eibl committed
177 178 179 180 181 182
void KDiff3Shell::slotNewInstance( const QString& fn1, const QString& fn2, const QString& fn3 )
{
   KDiff3Shell* pKDiff3Shell = new KDiff3Shell(false);
   ((KDiff3App*)pKDiff3Shell->m_part->widget())->completeInit(fn1,fn2,fn3);
}

Michael Reeves's avatar
Michael Reeves committed
183
#include "kdiff3_shell.moc"