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 <tdeaboutdata.h>
51#include <KoQueryTrader.h>
52#include <KoDocumentInfo.h>
53#include <KoDocument.h>
55#include <KoPartSelectDia.h>
56#include <KoFilterManager.h>
57#include <kiconloader.h>
59KoShellWindow::KoShellWindow()
60 : KoMainWindow( TDEGlobal::instance() )
62 m_activePage = m_lstPages.end();
64 m_pLayout =
new TQSplitter( centralWidget() );
67 m_pSidebar =
new IconSidePane( m_pLayout );
68 m_pSidebar->setSizePolicy( TQSizePolicy( TQSizePolicy::Maximum,
69 TQSizePolicy::Preferred ) );
70 m_pSidebar->setActionCollection( actionCollection() );
71 m_grpFile = m_pSidebar->insertGroup(i18n(
"Components"),
false,
this, TQ_SLOT( slotSidebar_Part(
int )));
72 m_grpDocuments = m_pSidebar->insertGroup(i18n(
"Documents"),
true,
this, TQ_SLOT(slotSidebar_Document(
int)));
73 m_pLayout->setResizeMode(m_pSidebar,TQSplitter::FollowSizeHint);
76 m_pFrame =
new KTabWidget( m_pLayout );
77 m_pFrame->setSizePolicy( TQSizePolicy( TQSizePolicy::Minimum,
78 TQSizePolicy::Preferred ) );
79 m_pFrame->setTabPosition( KTabWidget::Bottom );
81 m_tabCloseButton =
new TQToolButton( m_pFrame );
82 connect( m_tabCloseButton, TQ_SIGNAL( clicked() ),
83 this, TQ_SLOT( slotFileClose() ) );
84 m_tabCloseButton->setIconSet( SmallIconSet(
"tab_remove" ) );
85 m_tabCloseButton->adjustSize();
86 TQToolTip::add(m_tabCloseButton, i18n(
"Close"));
87 m_pFrame->setCornerWidget( m_tabCloseButton, BottomRight );
88 m_tabCloseButton->hide();
90 TQValueList<KoDocumentEntry> lstComponents = KoDocumentEntry::query(
false,TQString());
91 TQValueList<KoDocumentEntry>::Iterator it = lstComponents.begin();
94 for( ; it != lstComponents.end(); ++it )
96 KService* service = (*it).service();
97 if ( !service->genericName().isEmpty() )
99 id = m_pSidebar->insertItem(m_grpFile, service->icon(), service->genericName());
106 m_mapComponents[
id++ ] = *it;
109 TQValueList<int> list;
110 list.append( KoShellSettings::sidebarWidth() );
111 list.append( this->width() - KoShellSettings::sidebarWidth() );
112 m_pLayout->setSizes( list );
114 connect(
this, TQ_SIGNAL( documentSaved() ),
115 this, TQ_SLOT( slotNewDocumentName() ) );
117 connect( m_pFrame, TQ_SIGNAL( currentChanged( TQWidget* ) ),
118 this, TQ_SLOT( slotUpdatePart( TQWidget* ) ) );
119 connect( m_pFrame, TQ_SIGNAL( contextMenu(TQWidget * ,
const TQPoint &)),
this, TQ_SLOT( tab_contextMenu(TQWidget * ,
const TQPoint &)) );
121 m_client =
new KoShellGUIClient(
this );
125KoShellWindow::~KoShellWindow()
132 partManager()->setActivePart(0);
135 TQValueList<Page>::ConstIterator it = m_lstPages.begin();
136 for (; it != m_lstPages.end(); ++it )
138 (*it).m_pDoc->removeShell(
this );
139 delete (*it).m_pView;
140 if ( (*it).m_pDoc->viewCount() == 0 )
145 setRootDocumentDirect( 0L, TQPtrList<KoView>() );
149bool KoShellWindow::openDocumentInternal(
const KURL &url, KoDocument* )
166 KMimeType::Ptr mimeType = KMimeType::findByURL( url );
167 m_documentEntry = KoDocumentEntry::queryByMimeType( mimeType->name().latin1() );
169 KTempFile* tmpFile = 0;
172 if ( m_documentEntry.isEmpty() ) {
173 tmpFile =
new KTempFile;
175 KoFilterManager *manager =
new KoFilterManager( url.path() );
177 KoFilter::ConversionStatus status = manager->exp0rt( tmpFile->name(), mimetype );
180 if ( status != KoFilter::OK || mimetype.isEmpty() ) {
188 m_documentEntry = KoDocumentEntry::queryByMimeType( mimetype );
189 if ( m_documentEntry.isEmpty() ) {
196 tmpUrl.setPath( tmpFile->name() );
199 recentAction()->addURL( url );
201 KoDocument* newdoc = m_documentEntry.createDoc();
210 connect(newdoc, TQ_SIGNAL(sigProgress(
int)),
this, TQ_SLOT(slotProgress(
int)));
211 connect(newdoc, TQ_SIGNAL(completed()),
this, TQ_SLOT(slotKSLoadCompleted()));
212 connect(newdoc, TQ_SIGNAL(canceled(
const TQString & )),
this, TQ_SLOT(slotKSLoadCanceled(
const TQString & )));
213 newdoc->addShell(
this );
214 bool openRet = (!isImporting ()) ? newdoc->openURL(tmpUrl) : newdoc->import(tmpUrl);
217 newdoc->removeShell(
this);
234 newdoc->setMimeType( mimeType->name().latin1() );
235 newdoc->setOutputMimeType( mimeType->name().latin1() );
239 newdoc->setConfirmNonNativeSave(
true,
true);
240 newdoc->setConfirmNonNativeSave(
false,
true);
243 newdoc->setFile( url.path() );
246 newdoc->setURL( url );
257void KoShellWindow::slotSidebarItemClicked( TQIconViewItem *item )
262 int index = item->index();
265 m_documentEntry = m_mapComponents[ index ];
266 KoDocument *doc = m_documentEntry.createDoc();
271 if ( doc->showEmbedInitDialog(
this ) )
273 partManager()->addPart( doc,
false );
274 setRootDocument( doc );
283void KoShellWindow::slotKSLoadCompleted()
285 KoDocument* newdoc = (KoDocument *)(sender());
290 if (isImporting()) newdoc->resetURL ();
292 partManager()->addPart( newdoc,
false );
293 setRootDocument( newdoc );
294 disconnect(newdoc, TQ_SIGNAL(sigProgress(
int)),
this, TQ_SLOT(slotProgress(
int)));
295 disconnect(newdoc, TQ_SIGNAL(completed()),
this, TQ_SLOT(slotKSLoadCompleted()));
296 disconnect(newdoc, TQ_SIGNAL(canceled(
const TQString & )),
this, TQ_SLOT(slotKSLoadCanceled(
const TQString & )));
299void KoShellWindow::slotKSLoadCanceled(
const TQString & errMsg )
301 KMessageBox::error(
this, errMsg );
305 KoDocument* newdoc = (KoDocument *)(sender());
306 disconnect(newdoc, TQ_SIGNAL(sigProgress(
int)),
this, TQ_SLOT(slotProgress(
int)));
307 disconnect(newdoc, TQ_SIGNAL(completed()),
this, TQ_SLOT(slotKSLoadCompleted()));
308 disconnect(newdoc, TQ_SIGNAL(canceled(
const TQString & )),
this, TQ_SLOT(slotKSLoadCanceled(
const TQString & )));
311void KoShellWindow::saveAll()
313 KoView *currentView = (*m_activePage).m_pView;
314 for (TQValueList<Page>::iterator it=m_lstPages.begin(); it != m_lstPages.end(); ++it)
316 if ( (*it).m_pDoc->isModified() )
318 m_pFrame->showPage( (*it).m_pView );
319 (*it).m_pView->shell()->slotFileSave();
320 if ( (*it).m_pDoc->isModified() )
324 m_pFrame->showPage( currentView );
327void KoShellWindow::setRootDocument( KoDocument * doc )
329 kdDebug() <<
"KoShellWindow::setRootDocument this=" <<
this <<
" doc=" << doc << endl;
337 if ( !doc->shells().contains(
this ) )
338 doc->addShell(
this );
340 KoView *v = doc->createView(
this);
341 TQPtrList<KoView> views;
343 setRootDocumentDirect( doc, views );
345 v->setGeometry( 0, 0, m_pFrame->width(), m_pFrame->height() );
346 v->setPartManager( partManager() );
347 m_pFrame->addTab( v, TDEGlobal::iconLoader()->loadIcon( m_documentEntry.service()->icon(), TDEIcon::Small ), i18n(
"Untitled") );
354 page.m_id = m_pSidebar->insertItem( m_grpDocuments,
355 m_documentEntry.service()->icon(),
357 m_lstPages.append( page );
360 switchToPage( m_lstPages.fromLast() );
361 mnuSaveAll->setEnabled(
true);
364 setRootDocumentDirect( 0L, TQPtrList<KoView>() );
365 m_activePage = m_lstPages.end();
366 KoMainWindow::updateCaption();
370void KoShellWindow::slotNewDocumentName()
375void KoShellWindow::updateCaption()
378 KoMainWindow::updateCaption();
381 TQValueList<Page>::Iterator it = m_lstPages.begin();
382 for( ; it != m_lstPages.end() ; ++it )
384 if ( (*it).m_pDoc == rootDocument() )
389 if ( rootDocument()->documentInfo() )
391 name = rootDocument()->documentInfo()->title();
393 if ( name.isEmpty() )
395 name = rootDocument()->url().fileName();
397 if ( !name.isEmpty() )
399 if ( name.length() > 20 )
404 m_pFrame->changeTab( m_pFrame->currentPage(), name );
405 m_pSidebar->renameItem(m_grpDocuments, (*m_activePage).m_id, name);
414void KoShellWindow::slotSidebar_Part(
int _item)
417 kapp->setOverrideCursor( TQCursor(TQt::WaitCursor) );
418 m_documentEntry = m_mapComponents[ _item ];
419 kdDebug() << m_documentEntry.service() << endl;
420 kdDebug() << m_documentEntry.name() << endl;
421 KoDocument *doc = m_documentEntry.createDoc();
422 kapp->restoreOverrideCursor();
425 if ( doc->showEmbedInitDialog(
this ) )
427 partManager()->addPart( doc,
false );
428 setRootDocument( doc );
429 m_tabCloseButton->show();
436void KoShellWindow::slotSidebar_Document(
int _item)
439 if ( m_activePage != m_lstPages.end() &&
440 (*m_activePage).m_id == _item )
443 TQValueList<Page>::Iterator it = m_lstPages.begin();
444 while( it != m_lstPages.end() )
446 if ( (*it).m_id == _item )
455void KoShellWindow::slotShowSidebar()
457 if( m_pSidebar->isShown() )
460 m_pComponentsLabel->hide();
465 m_pComponentsLabel->show();
469void KoShellWindow::slotUpdatePart( TQWidget* widget )
471 KoView* v =
dynamic_cast<KoView*
>(widget);
474 TQValueList<Page>::Iterator it = m_lstPages.begin();
475 for( ; it != m_lstPages.end(); ++it )
477 if( (*it).m_pView == v )
483void KoShellWindow::switchToPage( TQValueList<Page>::Iterator it )
487 KoView *v = (*m_activePage).m_pView;
489 kdDebug() <<
" setting active part to " << (*m_activePage).m_pDoc << endl;
491 partManager()->setActivePart( (*m_activePage).m_pDoc, v );
493 TQPtrList<KoView> views;
495 setRootDocumentDirect( (*m_activePage).m_pDoc, views );
497 m_pSidebar->group(m_grpDocuments)->setSelected((*m_activePage).m_id,
true);
499 m_pFrame->showPage( v );
504 partSpecificHelpAction->setEnabled(
true);
505 partSpecificHelpAction->setText(i18n(
"%1 Handbook").arg((*m_activePage).m_pDoc->instance()->aboutData()->programName()));
508void KoShellWindow::slotFileNew()
510 m_documentEntry = KoPartSelectDia::selectPart(
this );
511 if ( m_documentEntry.isEmpty() )
513 KoDocument* newdoc = m_documentEntry.createDoc();
516 if ( !newdoc->showEmbedInitDialog(
this ) )
522 partManager()->addPart( newdoc,
false );
523 setRootDocument( newdoc );
524 m_tabCloseButton->show();
527void KoShellWindow::slotFileOpen()
529 KFileDialog *dialog=
new KFileDialog(TQString(), TQString(), 0L,
"file dialog",
true);
531 dialog->setCaption( i18n(
"Open Document") );
533 dialog->setCaption( i18n(
"Import Document") );
534 dialog->setMimeFilter( KoFilterManager::mimeFilter() );
537 if(dialog->exec()==TQDialog::Accepted) {
538 url=dialog->selectedURL();
539 recentAction()->addURL( url );
540 if ( url.isLocalFile() )
541 TDERecentDocument::add(url.path(-1));
543 TDERecentDocument::add(url.url(-1),
true);
552 (void) openDocumentInternal( url );
553 m_tabCloseButton->show();
556void KoShellWindow::slotFileClose()
561 if ( m_lstPages.count() == 0 )
566 if ( m_pFrame->count() == 0 )
567 m_tabCloseButton->hide();
570void KoShellWindow::closeDocument()
573 assert( m_activePage != m_lstPages.end() );
574 assert( rootDocument() == (*m_activePage).m_pDoc );
577 kdDebug() <<
"KoShellWindow::closeDocument calling standard queryClose" << endl;
578 if ( KoMainWindow::queryClose() )
580 kdDebug() <<
"Ok for closing document" << endl;
581 m_pSidebar->removeItem(m_grpDocuments, (*m_activePage).m_id );
582 (*m_activePage).m_pDoc->removeShell(
this);
583 Page oldPage = (*m_activePage);
584 m_lstPages.remove( m_activePage );
585 m_activePage = m_lstPages.end();
586 m_pSidebar->group(m_grpDocuments)->setSelected((*m_activePage).m_id,
true);
588 kdDebug() <<
"m_lstPages has " << m_lstPages.count() <<
" documents" << endl;
589 if ( m_lstPages.count() > 0 )
591 kdDebug() <<
"Activate the document behind" << endl;
592 switchToPage( m_lstPages.fromLast() );
596 kdDebug() <<
"Revert to initial state (no docs)" << endl;
597 setRootDocument( 0L );
598 partManager()->setActivePart( 0L, 0L );
599 mnuSaveAll->setEnabled(
false);
600 partSpecificHelpAction->setEnabled(
false);
601 partSpecificHelpAction->setText(i18n(
"Part Handbook"));
607 delete oldPage.m_pView;
608 if ( oldPage.m_pDoc->viewCount() <= 1 )
609 delete oldPage.m_pDoc;
612 kdDebug() <<
"m_lstPages has " << m_lstPages.count() <<
" documents" << endl;
615bool KoShellWindow::queryClose()
618 TQPtrList<KoView> currentViews;
619 KoDocument * currentDoc = 0L;
621 if (m_activePage != m_lstPages.end())
623 currentDoc = (*m_activePage).m_pDoc;
624 currentViews.append((*m_activePage).m_pView);
628 TQValueList<Page>::Iterator it = m_lstPages.begin();
629 for( ; it != m_lstPages.end(); ++it )
633 setRootDocumentDirect( (*it).m_pDoc, TQPtrList<KoView>() );
635 if ( !KoMainWindow::queryClose() )
643 setRootDocumentDirect( currentDoc, currentViews );
657void KoShellWindow::saveSettings()
659 KoShellSettings::setSidebarWidth( m_pLayout->sizes().first() );
660 KoShellSettings::writeConfig();
663TQString KoShellWindow::configFile()
const
669void KoShellWindow::tab_contextMenu(TQWidget * w,
const TQPoint &p)
673 int const mnuSave = menu.insertItem( il.loadIconSet(
"document-save", TDEIcon::Small ), i18n(
"Save") );
674 int const mnuClose = menu.insertItem( il.loadIcon(
"window-close", TDEIcon::Small ), i18n(
"Close") );
676 int tabnr = m_pFrame->indexOf( w );
677 Page page = m_lstPages[tabnr];
679 if ( !page.m_pDoc->isModified() )
680 menu.setItemEnabled( mnuSave,
false );
683 int const choice = menu.exec(p);
685 if( choice == mnuClose )
687 const int index = m_pFrame->currentPageIndex();
688 m_pFrame->setCurrentPage( tabnr );
690 if ( index > m_pFrame->currentPageIndex() )
691 m_pFrame->setCurrentPage(index-1);
693 m_pFrame->setCurrentPage(index);
695 else if ( choice == mnuSave )
697 page.m_pView->shell()->slotFileSave();
701void KoShellWindow::slotConfigureKeys()
703 KoView *view = rootView();
704 KKeyDialog dlg(
this );
705 dlg.insert( actionCollection() );
707 dlg.insert( view->actionCollection() );
708 if ( rootDocument() )
709 dlg.insert( rootDocument()->actionCollection() );
713void KoShellWindow::createShellGUI(
bool )
715 guiFactory()->addClient( m_client );
718void KoShellWindow::showPartSpecificHelp()
720 if((m_activePage == m_lstPages.end()) || ((*m_activePage).m_pDoc == 0))
723 kapp->invokeHelp(
"", (*m_activePage).m_pDoc->instance()->aboutData()->appName(),
"");
728KoShellGUIClient::KoShellGUIClient( KoShellWindow *window ) : KXMLGUIClient()
730 setXMLFile(
"koshellui.rc",
true,
true );
731 window->mnuSaveAll =
new TDEAction( i18n(
"Save All"), 0, window, TQ_SLOT( saveAll() ), actionCollection(),
"save_all" );
732 window->mnuSaveAll->setEnabled(
false);
733 window->partSpecificHelpAction =
new TDEAction(i18n(
"Part Handbook"),
"contents", 0, window, TQ_SLOT(showPartSpecificHelp()),
734 actionCollection(),
"partSpecificHelp");
735 window->partSpecificHelpAction->setEnabled(
false);
738#include "koshell_shell.moc"