24#include <tqsplitter.h>
25#include <tqiconview.h>
31#include "koshell_shell.h"
32#include "koshellsettings.h"
34#include <tdeapplication.h>
35#include <tdetempfile.h>
36#include <tdefiledialog.h>
39#include <kiconloader.h>
40#include <kkeydialog.h>
41#include <kstandarddirs.h>
42#include <klibloader.h>
43#include <tdepopupmenu.h>
45#include <tdemessagebox.h>
46#include <tderecentdocument.h>
47#include <tdeparts/partmanager.h>
49#include <tdeversion.h>
50#include <tdeaboutdata.h>
52#include <KoQueryTrader.h>
53#include <KoDocumentInfo.h>
54#include <KoDocument.h>
56#include <KoPartSelectDia.h>
57#include <KoFilterManager.h>
58#include <kiconloader.h>
60KoShellWindow::KoShellWindow()
61 : KoMainWindow( TDEGlobal::instance() )
63 m_activePage = m_lstPages.end();
65 m_pLayout =
new TQSplitter( centralWidget() );
68 m_pSidebar =
new IconSidePane( m_pLayout );
69 m_pSidebar->setSizePolicy( TQSizePolicy( TQSizePolicy::Maximum,
70 TQSizePolicy::Preferred ) );
71 m_pSidebar->setActionCollection( actionCollection() );
72 m_grpFile = m_pSidebar->insertGroup(i18n(
"Components"),
false,
this, TQ_SLOT( slotSidebar_Part(
int )));
73 m_grpDocuments = m_pSidebar->insertGroup(i18n(
"Documents"),
true,
this, TQ_SLOT(slotSidebar_Document(
int)));
74 m_pLayout->setResizeMode(m_pSidebar,TQSplitter::FollowSizeHint);
77 m_pFrame =
new KTabWidget( m_pLayout );
78 m_pFrame->setSizePolicy( TQSizePolicy( TQSizePolicy::Minimum,
79 TQSizePolicy::Preferred ) );
80 m_pFrame->setTabPosition( KTabWidget::Bottom );
82 m_tabCloseButton =
new TQToolButton( m_pFrame );
83 connect( m_tabCloseButton, TQ_SIGNAL( clicked() ),
84 this, TQ_SLOT( slotFileClose() ) );
85 m_tabCloseButton->setIconSet( SmallIconSet(
"tab_remove" ) );
86 m_tabCloseButton->adjustSize();
87 TQToolTip::add(m_tabCloseButton, i18n(
"Close"));
88 m_pFrame->setCornerWidget( m_tabCloseButton, BottomRight );
89 m_tabCloseButton->hide();
91 TQValueList<KoDocumentEntry> lstComponents = KoDocumentEntry::query(
false,TQString());
92 TQValueList<KoDocumentEntry>::Iterator it = lstComponents.begin();
95 for( ; it != lstComponents.end(); ++it )
97 KService* service = (*it).service();
98 if ( !service->genericName().isEmpty() )
100 id = m_pSidebar->insertItem(m_grpFile, service->icon(), service->genericName());
107 m_mapComponents[
id++ ] = *it;
110 TQValueList<int> list;
111 list.append( KoShellSettings::sidebarWidth() );
112 list.append( this->width() - KoShellSettings::sidebarWidth() );
113 m_pLayout->setSizes( list );
115 connect(
this, TQ_SIGNAL( documentSaved() ),
116 this, TQ_SLOT( slotNewDocumentName() ) );
118 connect( m_pFrame, TQ_SIGNAL( currentChanged( TQWidget* ) ),
119 this, TQ_SLOT( slotUpdatePart( TQWidget* ) ) );
120 connect( m_pFrame, TQ_SIGNAL( contextMenu(TQWidget * ,
const TQPoint &)),
this, TQ_SLOT( tab_contextMenu(TQWidget * ,
const TQPoint &)) );
122 m_client =
new KoShellGUIClient(
this );
126KoShellWindow::~KoShellWindow()
133 partManager()->setActivePart(0);
136 TQValueList<Page>::ConstIterator it = m_lstPages.begin();
137 for (; it != m_lstPages.end(); ++it )
139 (*it).m_pDoc->removeShell(
this );
140 delete (*it).m_pView;
141 if ( (*it).m_pDoc->viewCount() == 0 )
146 setRootDocumentDirect( 0L, TQPtrList<KoView>() );
150bool KoShellWindow::openDocumentInternal(
const KURL &url, KoDocument* )
167 KMimeType::Ptr mimeType = KMimeType::findByURL( url );
168 m_documentEntry = KoDocumentEntry::queryByMimeType( mimeType->name().latin1() );
170 KTempFile* tmpFile = 0;
173 if ( m_documentEntry.isEmpty() ) {
174 tmpFile =
new KTempFile;
176 KoFilterManager *manager =
new KoFilterManager( url.path() );
178 KoFilter::ConversionStatus status = manager->exp0rt( tmpFile->name(), mimetype );
181 if ( status != KoFilter::OK || mimetype.isEmpty() ) {
189 m_documentEntry = KoDocumentEntry::queryByMimeType( mimetype );
190 if ( m_documentEntry.isEmpty() ) {
197 tmpUrl.setPath( tmpFile->name() );
200 recentAction()->addURL( url );
202 KoDocument* newdoc = m_documentEntry.createDoc();
211 connect(newdoc, TQ_SIGNAL(sigProgress(
int)),
this, TQ_SLOT(slotProgress(
int)));
212 connect(newdoc, TQ_SIGNAL(completed()),
this, TQ_SLOT(slotKSLoadCompleted()));
213 connect(newdoc, TQ_SIGNAL(canceled(
const TQString & )),
this, TQ_SLOT(slotKSLoadCanceled(
const TQString & )));
214 newdoc->addShell(
this );
215 bool openRet = (!isImporting ()) ? newdoc->openURL(tmpUrl) : newdoc->import(tmpUrl);
218 newdoc->removeShell(
this);
235 newdoc->setMimeType( mimeType->name().latin1() );
236 newdoc->setOutputMimeType( mimeType->name().latin1() );
240 newdoc->setConfirmNonNativeSave(
true,
true);
241 newdoc->setConfirmNonNativeSave(
false,
true);
244 newdoc->setFile( url.path() );
247 newdoc->setURL( url );
258void KoShellWindow::slotSidebarItemClicked( TQIconViewItem *item )
263 int index = item->index();
266 m_documentEntry = m_mapComponents[ index ];
267 KoDocument *doc = m_documentEntry.createDoc();
272 if ( doc->showEmbedInitDialog(
this ) )
274 partManager()->addPart( doc,
false );
275 setRootDocument( doc );
284void KoShellWindow::slotKSLoadCompleted()
286 KoDocument* newdoc = (KoDocument *)(sender());
291 if (isImporting()) newdoc->resetURL ();
293 partManager()->addPart( newdoc,
false );
294 setRootDocument( newdoc );
295 disconnect(newdoc, TQ_SIGNAL(sigProgress(
int)),
this, TQ_SLOT(slotProgress(
int)));
296 disconnect(newdoc, TQ_SIGNAL(completed()),
this, TQ_SLOT(slotKSLoadCompleted()));
297 disconnect(newdoc, TQ_SIGNAL(canceled(
const TQString & )),
this, TQ_SLOT(slotKSLoadCanceled(
const TQString & )));
300void KoShellWindow::slotKSLoadCanceled(
const TQString & errMsg )
302 KMessageBox::error(
this, errMsg );
306 KoDocument* newdoc = (KoDocument *)(sender());
307 disconnect(newdoc, TQ_SIGNAL(sigProgress(
int)),
this, TQ_SLOT(slotProgress(
int)));
308 disconnect(newdoc, TQ_SIGNAL(completed()),
this, TQ_SLOT(slotKSLoadCompleted()));
309 disconnect(newdoc, TQ_SIGNAL(canceled(
const TQString & )),
this, TQ_SLOT(slotKSLoadCanceled(
const TQString & )));
312void KoShellWindow::saveAll()
314 KoView *currentView = (*m_activePage).m_pView;
315 for (TQValueList<Page>::iterator it=m_lstPages.begin(); it != m_lstPages.end(); ++it)
317 if ( (*it).m_pDoc->isModified() )
319 m_pFrame->showPage( (*it).m_pView );
320 (*it).m_pView->shell()->slotFileSave();
321 if ( (*it).m_pDoc->isModified() )
325 m_pFrame->showPage( currentView );
328void KoShellWindow::setRootDocument( KoDocument * doc )
330 kdDebug() <<
"KoShellWindow::setRootDocument this=" <<
this <<
" doc=" << doc << endl;
338 if ( !doc->shells().contains(
this ) )
339 doc->addShell(
this );
341 KoView *v = doc->createView(
this);
342 TQPtrList<KoView> views;
344 setRootDocumentDirect( doc, views );
346 v->setGeometry( 0, 0, m_pFrame->width(), m_pFrame->height() );
347 v->setPartManager( partManager() );
348 m_pFrame->addTab( v, TDEGlobal::iconLoader()->loadIcon( m_documentEntry.service()->icon(), TDEIcon::Small ), i18n(
"Untitled") );
355 page.m_id = m_pSidebar->insertItem( m_grpDocuments,
356 m_documentEntry.service()->icon(),
358 m_lstPages.append( page );
361 switchToPage( m_lstPages.fromLast() );
362 mnuSaveAll->setEnabled(
true);
365 setRootDocumentDirect( 0L, TQPtrList<KoView>() );
366 m_activePage = m_lstPages.end();
367 KoMainWindow::updateCaption();
371void KoShellWindow::slotNewDocumentName()
376void KoShellWindow::updateCaption()
379 KoMainWindow::updateCaption();
382 TQValueList<Page>::Iterator it = m_lstPages.begin();
383 for( ; it != m_lstPages.end() ; ++it )
385 if ( (*it).m_pDoc == rootDocument() )
390 if ( rootDocument()->documentInfo() )
392 name = rootDocument()->documentInfo()->title();
394 if ( name.isEmpty() )
396 name = rootDocument()->url().fileName();
398 if ( !name.isEmpty() )
400 if ( name.length() > 20 )
405 m_pFrame->changeTab( m_pFrame->currentPage(), name );
406 m_pSidebar->renameItem(m_grpDocuments, (*m_activePage).m_id, name);
415void KoShellWindow::slotSidebar_Part(
int _item)
418 kapp->setOverrideCursor( TQCursor(TQt::WaitCursor) );
419 m_documentEntry = m_mapComponents[ _item ];
420 kdDebug() << m_documentEntry.service() << endl;
421 kdDebug() << m_documentEntry.name() << endl;
422 KoDocument *doc = m_documentEntry.createDoc();
423 kapp->restoreOverrideCursor();
426 if ( doc->showEmbedInitDialog(
this ) )
428 partManager()->addPart( doc,
false );
429 setRootDocument( doc );
430 m_tabCloseButton->show();
437void KoShellWindow::slotSidebar_Document(
int _item)
440 if ( m_activePage != m_lstPages.end() &&
441 (*m_activePage).m_id == _item )
444 TQValueList<Page>::Iterator it = m_lstPages.begin();
445 while( it != m_lstPages.end() )
447 if ( (*it).m_id == _item )
456void KoShellWindow::slotShowSidebar()
458 if( m_pSidebar->isShown() )
461 m_pComponentsLabel->hide();
466 m_pComponentsLabel->show();
470void KoShellWindow::slotUpdatePart( TQWidget* widget )
472 KoView* v =
dynamic_cast<KoView*
>(widget);
475 TQValueList<Page>::Iterator it = m_lstPages.begin();
476 for( ; it != m_lstPages.end(); ++it )
478 if( (*it).m_pView == v )
484void KoShellWindow::switchToPage( TQValueList<Page>::Iterator it )
488 KoView *v = (*m_activePage).m_pView;
490 kdDebug() <<
" setting active part to " << (*m_activePage).m_pDoc << endl;
492 partManager()->setActivePart( (*m_activePage).m_pDoc, v );
494 TQPtrList<KoView> views;
496 setRootDocumentDirect( (*m_activePage).m_pDoc, views );
498 m_pSidebar->group(m_grpDocuments)->setSelected((*m_activePage).m_id,
true);
500 m_pFrame->showPage( v );
505 partSpecificHelpAction->setEnabled(
true);
506 partSpecificHelpAction->setText(i18n(
"%1 Handbook").arg((*m_activePage).m_pDoc->instance()->aboutData()->programName()));
509void KoShellWindow::slotFileNew()
511 m_documentEntry = KoPartSelectDia::selectPart(
this );
512 if ( m_documentEntry.isEmpty() )
514 KoDocument* newdoc = m_documentEntry.createDoc();
517 if ( !newdoc->showEmbedInitDialog(
this ) )
523 partManager()->addPart( newdoc,
false );
524 setRootDocument( newdoc );
525 m_tabCloseButton->show();
528void KoShellWindow::slotFileOpen()
530 KFileDialog *dialog=
new KFileDialog(TQString(), TQString(), 0L,
"file dialog",
true);
532 dialog->setCaption( i18n(
"Open Document") );
534 dialog->setCaption( i18n(
"Import Document") );
535 dialog->setMimeFilter( KoFilterManager::mimeFilter() );
538 if(dialog->exec()==TQDialog::Accepted) {
539 url=dialog->selectedURL();
540 recentAction()->addURL( url );
541 if ( url.isLocalFile() )
542 TDERecentDocument::add(url.path(-1));
544 TDERecentDocument::add(url.url(-1),
true);
553 (void) openDocumentInternal( url );
554 m_tabCloseButton->show();
557void KoShellWindow::slotFileClose()
562 if ( m_lstPages.count() == 0 )
567 if ( m_pFrame->count() == 0 )
568 m_tabCloseButton->hide();
571void KoShellWindow::closeDocument()
574 assert( m_activePage != m_lstPages.end() );
575 assert( rootDocument() == (*m_activePage).m_pDoc );
578 kdDebug() <<
"KoShellWindow::closeDocument calling standard queryClose" << endl;
579 if ( KoMainWindow::queryClose() )
581 kdDebug() <<
"Ok for closing document" << endl;
582 m_pSidebar->removeItem(m_grpDocuments, (*m_activePage).m_id );
583 (*m_activePage).m_pDoc->removeShell(
this);
584 Page oldPage = (*m_activePage);
585 m_lstPages.remove( m_activePage );
586 m_activePage = m_lstPages.end();
587 m_pSidebar->group(m_grpDocuments)->setSelected((*m_activePage).m_id,
true);
589 kdDebug() <<
"m_lstPages has " << m_lstPages.count() <<
" documents" << endl;
590 if ( m_lstPages.count() > 0 )
592 kdDebug() <<
"Activate the document behind" << endl;
593 switchToPage( m_lstPages.fromLast() );
597 kdDebug() <<
"Revert to initial state (no docs)" << endl;
598 setRootDocument( 0L );
599 partManager()->setActivePart( 0L, 0L );
600 mnuSaveAll->setEnabled(
false);
601 partSpecificHelpAction->setEnabled(
false);
602 partSpecificHelpAction->setText(i18n(
"Part Handbook"));
608 delete oldPage.m_pView;
609 if ( oldPage.m_pDoc->viewCount() <= 1 )
610 delete oldPage.m_pDoc;
613 kdDebug() <<
"m_lstPages has " << m_lstPages.count() <<
" documents" << endl;
616bool KoShellWindow::queryClose()
619 TQPtrList<KoView> currentViews;
620 KoDocument * currentDoc = 0L;
622 if (m_activePage != m_lstPages.end())
624 currentDoc = (*m_activePage).m_pDoc;
625 currentViews.append((*m_activePage).m_pView);
629 TQValueList<Page>::Iterator it = m_lstPages.begin();
630 for( ; it != m_lstPages.end(); ++it )
634 setRootDocumentDirect( (*it).m_pDoc, TQPtrList<KoView>() );
636 if ( !KoMainWindow::queryClose() )
644 setRootDocumentDirect( currentDoc, currentViews );
658void KoShellWindow::saveSettings()
660 KoShellSettings::setSidebarWidth( m_pLayout->sizes().first() );
661 KoShellSettings::writeConfig();
664TQString KoShellWindow::configFile()
const
670void KoShellWindow::tab_contextMenu(TQWidget * w,
const TQPoint &p)
674 int const mnuSave = menu.insertItem( il.loadIconSet(
"document-save", TDEIcon::Small ), i18n(
"Save") );
675 int const mnuClose = menu.insertItem( il.loadIcon(
"window-close", TDEIcon::Small ), i18n(
"Close") );
677 int tabnr = m_pFrame->indexOf( w );
678 Page page = m_lstPages[tabnr];
680 if ( !page.m_pDoc->isModified() )
681 menu.setItemEnabled( mnuSave,
false );
684 int const choice = menu.exec(p);
686 if( choice == mnuClose )
688 const int index = m_pFrame->currentPageIndex();
689 m_pFrame->setCurrentPage( tabnr );
691 if ( index > m_pFrame->currentPageIndex() )
692 m_pFrame->setCurrentPage(index-1);
694 m_pFrame->setCurrentPage(index);
696 else if ( choice == mnuSave )
698 page.m_pView->shell()->slotFileSave();
702void KoShellWindow::slotConfigureKeys()
704 KoView *view = rootView();
705 KKeyDialog dlg(
this );
706 dlg.insert( actionCollection() );
708 dlg.insert( view->actionCollection() );
709 if ( rootDocument() )
710 dlg.insert( rootDocument()->actionCollection() );
714void KoShellWindow::createShellGUI(
bool )
716 guiFactory()->addClient( m_client );
719void KoShellWindow::showPartSpecificHelp()
721 if((m_activePage == m_lstPages.end()) || ((*m_activePage).m_pDoc == 0))
724 kapp->invokeHelp(
"", (*m_activePage).m_pDoc->instance()->aboutData()->appName(),
"");
729KoShellGUIClient::KoShellGUIClient( KoShellWindow *window ) : KXMLGUIClient()
731 setXMLFile(
"koshellui.rc",
true,
true );
732 window->mnuSaveAll =
new TDEAction( i18n(
"Save All"), 0, window, TQ_SLOT( saveAll() ), actionCollection(),
"save_all" );
733 window->mnuSaveAll->setEnabled(
false);
734 window->partSpecificHelpAction =
new TDEAction(i18n(
"Part Handbook"),
"contents", 0, window, TQ_SLOT(showPartSpecificHelp()),
735 actionCollection(),
"partSpecificHelp");
736 window->partSpecificHelpAction->setEnabled(
false);
739#include "koshell_shell.moc"