
#include "kryomol.h"

//#include <kkeydialog.h>
#include <kfiledialog.h>
#include <kconfig.h>
#include <kurl.h>

#include <kedittoolbar.h>
#include <kiconloader.h>
#include <ktoolbar.h>

#include <kaction.h>
#include <ktoggleaction.h>
#include <kactioncollection.h>
#include <kstandardaction.h>

#include <klibloader.h>
#include <kmessagebox.h>
#include <kstatusbar.h>
#include <klocale.h>
#include <KShortcutsDialog>
#include <QDesktopWidget>
#include <QGLWidget>

KryoMol::KryoMol()
    : KParts::MainWindow( 0L, "kryomol" )
{
  // set the shell's ui resource file
  
  setXMLFile("kryomolui.rc");
  setupActions();
  statusBar()->show();

  // and a status bar


  // 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
 KPluginFactory *factory = KPluginLoader("kryomolpart").factory();

  if (factory)
  {
    // now that the Part is loaded, we cast it to a Part to get
    // our hands on it
    QStringList pl;
    pl << "KParts::ReadOnlyPart";
    m_part = factory->create< KParts::ReadOnlyPart >( this );


    if (m_part)
    {
      // tell the KParts::MainWindow that this is indeed the main widget
    //  setCentralWidget( new QGLWidget(this) );//m_part->widget());
      setCentralWidget( m_part->widget() );
      setupGUI(qApp->desktop()->availableGeometry().size()*0.7);
      // and integrate the part's GUI with the shell's
      createGUI(m_part);    // then, setup our actions
      connect(this,SIGNAL(paste()),m_part,SLOT(OnPaste()));
      connect(this,SIGNAL(close()),m_part,SLOT(OnCloseCurrent()));
      connect(this,SIGNAL(reload()),m_part,SLOT(OpenFile()));
    }
  }
  else
  {
    // if we couldn't find our Part, we exit since the Shell by
    // itself can't do anything useful
    KMessageBox::error(this, i18n("Could not find our part."));
    kapp->quit();
    // we return here, cause kapp->quit() only means "exit the
    // next time we enter the event loop...
    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();
 // setXMLFile("kryomol/kryomolui.rc");


}

KryoMol::~KryoMol()
{}


void KryoMol::setupActions()
{
  actionCollection()->addAction(KStandardAction::Open,this,SLOT(OnFileOpen()));
  actionCollection()->addAction(KStandardAction::Close,this,SLOT(OnFileClose()));


  KIconLoader *loader = KIconLoader::global();
  KAction* reloadAction = actionCollection()->addAction("reload");
  reloadAction->setText(i18n("Reload"));
  reloadAction->setShortcut(Qt::Key_F5);
  reloadAction->setIconSet(loader->loadIcon("reload_page",KIconLoader::Toolbar));
  connect(reloadAction,SIGNAL(triggered()),this,SLOT(OnReload()));
   

  KStandardAction::paste(this,SLOT(OnPaste()), actionCollection());
  KStandardAction::quit(kapp, SLOT(quit()), actionCollection());

}

void KryoMol::saveProperties(KConfig* /*config*/)
{
  // the 'config' object points to the session managed
  // config file.  anything you write here will be available
  // later when this app is restored
}

void KryoMol::readProperties(KConfig* /*config*/)
{
  // 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'
}


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

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

void KryoMol::optionsConfigureKeys()
{
  KShortcutsDialog::configure(actionCollection());
}


void KryoMol::OnFileOpen()
{
  KUrl url = KFileDialog::getOpenUrl( QString() , QString(), this );

   LoadUrl(url);
}

void KryoMol::OnFileClose()
{
  emit close();
}

void KryoMol::OnPaste()
{
  emit paste();
}

void KryoMol::LoadUrl(const KUrl& url)
{
  m_part->openUrl(url);
}

void KryoMol::OnReload()
{
  emit reload();
}
#include "kryomol.moc"
