20 #include <QtCore/QTextStream>
21 #include <QtGui/QDockWidget>
23 #include <ktexteditor/document.h>
24 #include <ktexteditor/view.h>
26 #include <kedittoolbar.h>
27 #include <kencodingfiledialog.h>
28 #include <kiconloader.h>
29 #include <kshortcutsdialog.h>
30 #include <klibloader.h>
32 #include <kmessagebox.h>
33 #include <kparts/componentfactory.h>
34 #include <ksqueezedtextlabel.h>
35 #include <kstatusbar.h>
36 #include <kstandardaction.h>
37 #include <kmimetypetrader.h>
38 #include <kservicetypetrader.h>
39 #include <ktoggleaction.h>
41 #include <kactioncollection.h>
46 #define ID_N_OF_N_DIFFERENCES 1
47 #define ID_N_OF_N_FILES 2
51 : KParts::MainWindow( ),
55 if ( !initialGeometrySet() )
59 setXMLFile(
"kompareui.rc");
65 m_viewPart = KMimeTypeTrader::createInstanceFromQuery<KParts::ReadWritePart>(
"text/x-patch",
"Kompare/ViewPart",
this);
69 setCentralWidget( m_viewPart->widget() );
71 createGUI(m_viewPart);
77 KMessageBox::error(
this, i18n(
"Could not load our KompareViewPart." ) );
81 m_navTreeDock =
new QDockWidget( i18n(
"Navigation" ),
this );
85 m_navTreePart = KServiceTypeTrader::createInstanceFromQuery<KParts::ReadOnlyPart>
86 (
"KParts/ReadOnlyPart",
"'Kompare/NavigationPart' in ServiceTypes", m_navTreeDock);
90 m_navTreeDock->
setWidget( m_navTreePart->widget() );
91 addDockWidget( Qt::TopDockWidgetArea, m_navTreeDock );
98 KMessageBox::error(
this, i18n(
"Could not load our KompareNavigationPart." ) );
103 connect( m_viewPart, SIGNAL( modelsChanged(
const Diff2::DiffModelList*) ),
104 m_navTreePart, SLOT( slotModelsChanged(
const Diff2::DiffModelList*) ) );
106 connect( m_viewPart, SIGNAL( kompareInfo(Kompare::Info*) ),
107 m_navTreePart, SLOT( slotKompareInfo(Kompare::Info*) ) );
109 connect( m_navTreePart, SIGNAL( selectionChanged(
const Diff2::DiffModel*,
const Diff2::Difference*) ),
110 m_viewPart, SIGNAL( selectionChanged(
const Diff2::DiffModel*,
const Diff2::Difference*) ) );
111 connect( m_viewPart, SIGNAL( setSelection(
const Diff2::DiffModel*,
const Diff2::Difference*) ),
112 m_navTreePart, SLOT( slotSetSelection(
const Diff2::DiffModel*,
const Diff2::Difference*) ) );
114 connect( m_navTreePart, SIGNAL( selectionChanged(
const Diff2::Difference*) ),
115 m_viewPart, SIGNAL( selectionChanged(
const Diff2::Difference*) ) );
116 connect( m_viewPart, SIGNAL( setSelection(
const Diff2::Difference*) ),
117 m_navTreePart, SLOT( slotSetSelection(
const Diff2::Difference*) ) );
125 connect( m_viewPart, SIGNAL(applyDifference(
bool)),
126 m_navTreePart, SLOT(slotApplyDifference(
bool)) );
127 connect( m_viewPart, SIGNAL(applyAllDifferences(
bool)),
128 m_navTreePart, SLOT(slotApplyAllDifferences(
bool)) );
129 connect( m_viewPart, SIGNAL(applyDifference(
const Diff2::Difference*,
bool)),
130 m_navTreePart, SLOT(slotApplyDifference(
const Diff2::Difference*,
bool)) );
133 connect( m_viewPart, SIGNAL( setStatusBarModelInfo(
int,
int,
int,
int,
int ) ),
135 connect( m_viewPart, SIGNAL( setStatusBarText(
const QString&) ),
136 this, SLOT( slotSetStatusBarText(
const QString&) ) );
138 connect( m_viewPart, SIGNAL(diffString(
const QString&)),
139 this, SLOT(slotSetDiffString(
const QString&)) );
142 setAutoSaveSettings(
"General Options" );
151 bool rv = m_viewPart->queryClose();
159 kDebug(8102) <<
"Url = " << url.prettyUrl() << endl;
166 kDebug(8102) <<
"Using stdin to read the diff" << endl;
168 file.
open( stdin, QIODevice::ReadOnly );
181 m_sourceURL = source;
182 m_destinationURL = destination;
190 m_destinationURL = diff;
195 void KompareShell::setupActions()
198 a = actionCollection()->addAction(KStandardAction::Open,
this, SLOT(slotFileOpen()));
199 a->setText( i18n(
"&Open Diff..." ) );
200 a = actionCollection()->addAction(
"file_compare_files",
this, SLOT(slotFileCompareFiles()));
201 a->setIcon(KIcon(
"document-open"));
202 a->setText(i18n(
"&Compare Files..."));
204 a = actionCollection()->addAction(
"file_blend_url",
this, SLOT(slotFileBlendURLAndDiff()));
205 a->setText(i18n(
"&Blend URL with Diff..."));
207 actionCollection()->addAction(KStandardAction::Quit,
this, SLOT( slotFileClose() ));
209 createStandardStatusBarAction();
210 setStandardToolBarMenuEnabled(
true);
211 m_showTextView =
new KToggleAction(i18n(
"Show T&ext View"),
this);
214 actionCollection()->addAction(
"options_show_text_view", m_showTextView);
215 connect(m_showTextView, SIGNAL(triggered(
bool)), SLOT(slotShowTextView()));
217 KStandardAction::keyBindings(
this, SLOT(optionsConfigureKeys()), actionCollection());
218 KStandardAction::configureToolbars(
this, SLOT(optionsConfigureToolbars()), actionCollection());
221 void KompareShell::setupStatusBar()
225 statusBar()->insertPermanentItem( i18n(
" 0 of 0 files "),
ID_N_OF_N_FILES, 0);
227 m_generalLabel =
new KSqueezedTextLabel(
"", 0 );
228 statusBar()->addWidget( m_generalLabel, 1 );
229 m_generalLabel->setAlignment( Qt::AlignLeft );
234 kDebug(8102) <<
"KompareShell::updateStatusBar()" << endl;
239 if ( modelIndex >= 0 )
240 fileStr = i18np(
" %2 of %1 file ",
" %2 of %1 files ", modelCount, modelIndex + 1 );
242 fileStr = i18np(
" %1 file ",
" %1 files ", modelCount );
244 if ( differenceIndex >= 0 )
245 diffStr = i18np(
" %2 of %1 difference, %3 applied ",
" %2 of %1 differences, %3 applied ", differenceCount ,
246 differenceIndex + 1, appliedCount );
248 diffStr = i18np(
" %1 difference ",
" %1 differences ", differenceCount );
254 void KompareShell::slotSetStatusBarText(
const QString& text )
256 m_generalLabel->setText( text );
264 if ( m_mode == Kompare::ComparingFiles )
266 config.writeEntry(
"Mode",
"ComparingFiles" );
267 config.writePathEntry(
"SourceUrl", m_sourceURL.url() );
268 config.writePathEntry(
"DestinationUrl", m_destinationURL.url() );
270 else if ( m_mode == Kompare::ShowingDiff )
272 config.writeEntry(
"Mode",
"ShowingDiff" );
273 config.writePathEntry(
"DiffUrl", m_diffURL.url() );
286 QString mode = config.readEntry(
"Mode",
"ComparingFiles" );
287 if ( mode ==
"ComparingFiles" )
289 m_mode = Kompare::ComparingFiles;
290 m_sourceURL = config.readPathEntry(
"SourceUrl",
"" );
291 m_destinationURL = config.readPathEntry(
"DestinationFile",
"" );
297 else if ( mode ==
"ShowingDiff" )
299 m_mode = Kompare::ShowingDiff;
300 m_diffURL = config.readPathEntry(
"DiffUrl",
"" );
304 m_viewPart->openUrl( m_diffURL );
314 void KompareShell::slotFileOpen()
317 KUrl url = KFileDialog::getOpenUrl( KUrl(),
"text/x-patch",
this );
318 if( !url.isEmpty() ) {
326 void KompareShell::slotFileBlendURLAndDiff()
330 dialog.setCaption( i18n(
"Blend File/Folder with diff Output" ) );
331 dialog.setFirstGroupBoxTitle( i18n(
"File/Folder" ) );
332 dialog.setSecondGroupBoxTitle( i18n(
"Diff Output" ) );
334 KGuiItem blendGuiItem( i18n(
"Blend" ),
QString(), i18n(
"Blend this file or folder with the diff output" ), i18n(
"If you have entered a file or folder name and a file that contains diff output in the fields in this dialog then this button will be enabled and pressing it will open kompare's main view where the output of the entered file or files from the folder are mixed with the diff output so you can then apply the difference(s) to a file or to the files. " ) );
335 dialog.setButtonGuiItem( KDialog::Ok, blendGuiItem );
337 dialog.setGroup(
"Recent Blend Files" );
339 dialog.setFirstURLRequesterMode( KFile::File|KFile::Directory|KFile::ExistingOnly );
341 dialog.setSecondURLRequesterMode( KFile::File|KFile::ExistingOnly );
342 if ( dialog.exec() == QDialog::Accepted )
344 m_sourceURL = dialog.getFirstURL();
345 m_destinationURL = dialog.getSecondURL();
351 shell->
blend( m_sourceURL, m_destinationURL );
355 void KompareShell::slotFileCompareFiles()
359 dialog.setCaption( i18n(
"Compare Files or Folders" ) );
360 dialog.setFirstGroupBoxTitle( i18n(
"Source" ) );
361 dialog.setSecondGroupBoxTitle( i18n(
"Destination" ) );
363 KGuiItem compareGuiItem( i18n(
"Compare" ),
QString(), i18n(
"Compare these files or folders" ), i18n(
"If you have entered 2 filenames or 2 folders in the fields in this dialog then this button will be enabled and pressing it will start a comparison of the entered files or folders. " ) );
364 dialog.setButtonGuiItem( KDialog::Ok, compareGuiItem );
366 dialog.setGroup(
"Recent Compare Files" );
368 dialog.setFirstURLRequesterMode( KFile::File|KFile::Directory|KFile::ExistingOnly );
369 dialog.setSecondURLRequesterMode( KFile::File|KFile::Directory|KFile::ExistingOnly );
371 if ( dialog.exec() == QDialog::Accepted )
373 m_sourceURL = dialog.getFirstURL();
374 m_destinationURL = dialog.getSecondURL();
378 kDebug(8102) <<
"The encoding is: " << dialog.encoding() << endl;
380 shell->
compare( m_sourceURL, m_destinationURL );
384 void KompareShell::slotFileClose()
386 if ( m_viewPart->queryClose() )
392 void KompareShell::slotShowTextView()
394 if ( !m_textViewWidget )
399 m_textViewWidget =
new QDockWidget( i18n(
"Text View" ),
this );
402 m_textViewPart = KServiceTypeTrader::createInstanceFromQuery<KTextEditor::Document>(
404 this,
this,
QString(), QVariantList(), &error );
405 if ( m_textViewPart )
407 m_textView = qobject_cast<KTextEditor::View*>( m_textViewPart->createView(
this ) );
408 m_textViewWidget->
setWidget( static_cast<QWidget*>(m_textView) );
409 m_textViewPart->setHighlightingMode(
"Diff" );
410 m_textViewPart->setText( m_diffString );
412 m_textViewWidget->
show();
413 connect( m_textViewWidget, SIGNAL(visibilityChanged(
bool)), SLOT(slotVisibilityChanged(
bool)) );
415 else if ( m_textViewWidget->
isVisible() )
416 m_textViewWidget->
hide();
418 m_textViewWidget->
show();
420 addDockWidget( Qt::BottomDockWidgetArea, m_textViewWidget );
424 void KompareShell::slotVisibilityChanged(
bool visible )
426 m_showTextView->setChecked( visible );
429 void KompareShell::slotSetDiffString(
const QString& diffString )
431 if ( m_textViewPart )
432 m_textViewPart->setText( diffString );
433 m_diffString = diffString;
436 void KompareShell::optionsConfigureKeys()
438 KShortcutsDialog dlg( KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed,
this );
440 dlg.addCollection( actionCollection() );
442 dlg.addCollection( m_viewPart->actionCollection() );
447 void KompareShell::optionsConfigureToolbars()
449 KConfigGroup group(KGlobal::config(), autoSaveGroup());
450 saveMainWindowSettings(group);
452 KEditToolBar dlg(factory());
453 connect(&dlg,SIGNAL(newToolbarConfig()),
this,SLOT(newToolbarConfig()));
457 void KompareShell::newToolbarConfig()
459 KConfigGroup group(KGlobal::config(), autoSaveGroup());
460 applyMainWindowSettings(group);
468 #include "kompare_shell.moc"
virtual bool queryClose()
Definition of class KompareURLDialog.
virtual void setEncoding(const QString &encoding)
This will set the encoding to use for all files that are read or for the diffoutput.
virtual void compareFiles(const KUrl &sourceFile, const KUrl &destinationFile)=0
Compare, with diff, source with destination files.
void blend(const KUrl &url1, const KUrl &diff)
Use this method to blend diff into url1 (file or directory)
void openStdin()
Use this method to load the diff from stdin.
virtual int saveProperties(KConfig *config)=0
void setObjectName(const QString &name)
void slotUpdateStatusBar(int modelIndex, int differenceIndex, int modelCount, int differenceCount, int appliedCount)
void saveProperties(KConfigGroup &)
This method is called when it is time for the app to save its properties for session management purpo...
virtual int readProperties(KConfig *config)=0
Warning this should be in class Part in KDE 4.0, not here ! Around that time the methods will disappe...
virtual void openDirAndDiff(const KUrl &dir, const KUrl &diffFile)=0
This will show the directory and the directory with the diff applied.
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
virtual ~KompareShell()
Default Destructor.
void readProperties(const KConfigGroup &)
This method is called when this app is restored.
#define ID_N_OF_N_DIFFERENCES
KompareInterface * viewPart() const
void compare(const KUrl &source, const KUrl &destination)
Use this method to compare 2 URLs (files or directories)
virtual bool openDiff(const KUrl &diffUrl)=0
Open and parse the diff file at url.
QString fromLatin1(const char *str, int size)
void openDiff(const KUrl &url)
Use this method to load whatever file/URL you have.
virtual void compare(const KUrl &sourceFile, const KUrl &destinationFile)=0
Compare, with diff, source with destination, can also be used if you do not know what source and dest...
This is the application "Shell".
KompareShell()
Default Constructor.