diff --git a/modules/gui/src/CMakeLists.txt b/modules/gui/src/CMakeLists.txt index 4b5a993336cdafe330964548a83702282621e5d7..f9698b9d3cf061a0cea86e870170c4f8216bbbdc 100644 --- a/modules/gui/src/CMakeLists.txt +++ b/modules/gui/src/CMakeLists.txt @@ -134,6 +134,7 @@ endif() set(OST_GUI_HEADERS change_process_name.hh file_browser.hh +file_type_dialog.hh remote_loader.hh gl_canvas.hh gl_win.hh @@ -166,6 +167,7 @@ thin_splitter.cc perspective.cc widget_registry.cc file_browser.cc +file_type_dialog.cc remote_loader.cc main.cc mdi_sub_window.cc @@ -276,6 +278,7 @@ scene_win/scene_win_model.cc set(HEADERS_TO_BE_MOCCED file_browser.hh +file_type_dialog.hh remote_loader.hh gl_canvas.hh gl_win.hh diff --git a/modules/gui/src/file_loader.cc b/modules/gui/src/file_loader.cc index 49daa60504c455d8b95d4a0db944b817486eb906..ffce1169b7a0e40e6800f3ec743eb18417dd97be 100644 --- a/modules/gui/src/file_loader.cc +++ b/modules/gui/src/file_loader.cc @@ -30,9 +30,11 @@ #include <ost/mol/mol.hh> #include <ost/mol/surface.hh> +#include <ost/io/io_manager.hh> #include <ost/io/mol/pdb_reader.hh> #include <ost/io/mol/load_entity.hh> #include <ost/io/mol/load_surface.hh> +#include <ost/io/mol/entity_io_pdb_handler.hh> #include <ost/conop/conop.hh> @@ -44,6 +46,7 @@ #include <ost/gui/perspective.hh> #include <ost/gui/python_shell/python_interpreter.hh> #include <ost/gui/main_area.hh> +#include <ost/gui/file_type_dialog.hh> #if OST_IMG_ENABLED #include <ost/io/img/load_map.hh> @@ -75,22 +78,22 @@ void FileLoader::LoadObject(const QString& file_name) } else{ obj=FileLoader::TryLoadEntity(file_name); +#if OST_IMG_ENABLED if (!obj) { try{ obj=FileLoader::TryLoadMap(file_name); - if (!obj) return; } catch (io::IOException&) { - obj = gfx::GfxObjP(); + return; } } +#endif if (!obj) { obj=FileLoader::TryLoadSurface(file_name); } if (!obj) { - QMessageBox messageBox(QMessageBox::Warning, "Error while Loading Entity", - "Could not Open file. No suitable io handler found!"); - messageBox.setStandardButtons( QMessageBox::Ok); - messageBox.exec(); + obj = FileLoader::NoHandlerFound(file_name); + } + if (!obj){ return; } try{ @@ -105,6 +108,25 @@ void FileLoader::LoadObject(const QString& file_name) } } +gfx::GfxObjP FileLoader::NoHandlerFound(const QString& filename){ + FileTypeDialog dialog(filename); + if(dialog.exec()){ + if(dialog.GetEntityHandler()){ + return TryLoadEntity(filename, dialog.GetEntityHandler()); + } + if(dialog.GetSurfaceHandler()){ + return TryLoadSurface(filename,dialog.GetSurfaceHandler()); + } +#if OST_IMG_ENABLED + if(dialog.GetMapHandler()){ + return TryLoadMap(filename,dialog.GetMapHandler()); + } +#endif + } + + return gfx::GfxObjP(); +} + void FileLoader::LoadFrom(const QString& id, const QString& site) { if(!loader_manager_.get()) @@ -150,60 +172,114 @@ void FileLoader::HandleError(Message m, gfx::GfxObjP obj){ } } -gfx::GfxObjP FileLoader::TryLoadEntity(const QString& filename) +gfx::GfxObjP FileLoader::TryLoadEntity(const QString& file_name, io::EntityIOHandlerP handler) { - try { - mol::EntityHandle ent=io::LoadEntity(filename.toStdString()); - QFileInfo file_info(filename); - gfx::EntityP gfx_ent(new gfx::Entity(file_info.baseName().toStdString(), - ent)); - return gfx_ent; - } catch (io::IOException& e) { - return gfx::GfxObjP(); + if(!handler){ + try{ + handler = io::IOManager::Instance().FindEntityImportHandler(file_name.toStdString()); + } + catch(io::IOException e){ + handler = io::EntityIOHandlerP(); + } + } + if(handler){ + if(dynamic_cast<io::EntityIOPDBHandler*>(handler.get())){ + FileLoader::LoadPDB(file_name); + } + else{ + QFileInfo file_info(file_name); + mol::EntityHandle eh=mol::CreateEntity(); + mol::XCSEditor xcs_lock=eh.RequestXCSEditor(mol::BUFFERED_EDIT); + try{ + handler->Import(eh,file_name.toStdString()); + if(handler->RequiresBuilder()) { + conop::BuilderP builder = conop::Conopology::Instance().GetBuilder(); + conop::Conopology::Instance().ConnectAll(builder,eh,0); + } + gfx::GfxObjP obj(new gfx::Entity(file_info.baseName().toStdString(),eh)); + return obj; + } + catch(io::IOException e){ + QMessageBox messageBox(QMessageBox::Warning, + "Error while loading file", e._mesg.c_str()); + messageBox.setStandardButtons( QMessageBox::Ok); + messageBox.exec(); + } + } } + return gfx::GfxObjP(); } -gfx::GfxObjP FileLoader::TryLoadMap(const QString& filename) throw(io::IOException) -{ -//TODO IMPROVE CODE #if OST_IMG_ENABLED - img::ImageHandle map=io::LoadMap(filename.toStdString()); - ost::img::Extent ext = map.GetExtent(); - if(ext.GetSize().GetDepth()>1){ - QFileInfo file_info(filename); - gfx::MapIsoP map_iso(new gfx::MapIso(file_info.baseName().toStdString(), - map, 0.0)); - map_iso->SetLevel(map_iso->GetMean()); - return map_iso; +gfx::GfxObjP FileLoader::TryLoadMap(const QString& filename, io::MapIOHandlerPtr handler) throw(io::IOException) +{ + if(!handler){ + try{ + handler = io::IOManager::Instance().FindMapImportHandlerFile(filename.toStdString(),io::UndefinedImageFormat()); + } + catch(io::IOException e){ + handler = io::MapIOHandlerPtr(); + } } - else if(ext.GetSize().GetDepth()==1){ - //FIXME ImageHandle should not be destroyed at the end of method - //therefore hack with list - loaded_images_.append(map); - ost::img::gui::DataViewer* viewer = GostyApp::Instance()->CreateDataViewer(loaded_images_.last()); - gui::GostyApp::Instance()->GetPerspective()->GetMainArea()->AddWidget(filename,viewer); + if(handler){ + img::ImageHandle map = CreateImage(img::Extent(),img::REAL,img::SPATIAL); + try{ + handler->Import(map,filename.toStdString(),io::UndefinedImageFormat()); + ost::img::Extent ext = map.GetExtent(); + if(ext.GetSize().GetDepth()>1){ + QFileInfo file_info(filename); + gfx::MapIsoP map_iso(new gfx::MapIso(file_info.baseName().toStdString(), + map, 0.0)); + map_iso->SetLevel(map_iso->GetMean()); + return map_iso; + } + else if(ext.GetSize().GetDepth()==1){ + //FIXME ImageHandle should not be destroyed at the end of method + //therefore hack with list + loaded_images_.append(map); + ost::img::gui::DataViewer* viewer = GostyApp::Instance()->CreateDataViewer(loaded_images_.last()); + gui::GostyApp::Instance()->GetPerspective()->GetMainArea()->AddWidget(filename,viewer); + throw io::IOException("File already loaded"); + } + }catch(io::IOException e){ + QMessageBox messageBox(QMessageBox::Warning, + "Error while loading file", e._mesg.c_str()); + messageBox.setStandardButtons( QMessageBox::Ok); + messageBox.exec(); + } } -#else - throw io::IOException("No IPLT Available"); -#endif return gfx::GfxObjP(); } +#endif -gfx::GfxObjP FileLoader::TryLoadSurface(const QString& filename) +gfx::GfxObjP FileLoader::TryLoadSurface(const QString& filename, io::SurfaceIOHandlerPtr handler) { - try { - QFileInfo fi(filename); - QString path = fi.absolutePath().append(QDir::separator()).append(fi.completeBaseName()); - mol::EntityHandle ent = io::LoadEntity(path.toStdString() + ".pdb"); - mol::SurfaceHandle surf= io::LoadSurface(path.toStdString(),"msms"); - surf.Attach(ent,5.0); - gfx::SurfaceP gfx_surf(new gfx::Surface(fi.baseName().toStdString(),surf)); - - return gfx_surf; - }catch (io::IOException&) { - return gfx::GfxObjP(); + if(!handler){ + try{ + handler = io::IOManager::Instance().FindSurfaceImportHandler(filename.toStdString(),"msms"); + } + catch(io::IOException e){ + handler = io::SurfaceIOHandlerPtr(); + } } - + if(handler){ + try { + QFileInfo fi(filename); + QString path = fi.absolutePath().append(QDir::separator()).append(fi.completeBaseName()); + mol::SurfaceHandle sh = mol::CreateSurface(); + handler->Import(sh,filename.toStdString()); + QString pdb_path(path + ".pdb"); + if(QFile::exists(pdb_path)){ + mol::EntityHandle ent = io::LoadEntity(pdb_path.toStdString()); + sh.Attach(ent,5.0); + gfx::SurfaceP gfx_surf(new gfx::Surface(fi.baseName().toStdString(),sh)); + return gfx_surf; + } + }catch (io::IOException&) { + return gfx::GfxObjP(); + } + } + return gfx::GfxObjP(); } void FileLoader::RunScript(const QString& filename) @@ -259,7 +335,6 @@ void FileLoader::LoadPDB(const QString& filename) } } } - FileLoader::~FileLoader(){} } } diff --git a/modules/gui/src/file_loader.hh b/modules/gui/src/file_loader.hh index f61ebab28024c9d47725428881d93a0216697f3b..fa158ff456920cc59af0ee420f7df0fa63aadaa1 100644 --- a/modules/gui/src/file_loader.hh +++ b/modules/gui/src/file_loader.hh @@ -25,10 +25,17 @@ #include <QMap> #include <ost/gfx/gfx_object.hh> + #include <ost/gui/module_config.hh> #include <ost/gui/remote_site_loader.hh> #include <ost/gui/loader_manager.hh> + #include <ost/io/io_exception.hh> +#include <ost/io/entity_io_handler.hh> +#include <ost/io/surface_io_handler.hh> +#if OST_IMG_ENABLED +#include <ost/io/map_io_handler.hh> +#endif namespace ost { namespace gui { @@ -36,11 +43,14 @@ class DLLEXPORT_OST_GUI FileLoader { private: FileLoader(); static void HandleError(Message m, gfx::GfxObjP obj); - static gfx::GfxObjP TryLoadEntity(const QString& filename); - static gfx::GfxObjP TryLoadMap(const QString& filename) throw(io::IOException); - static gfx::GfxObjP TryLoadSurface(const QString& filename); + static gfx::GfxObjP TryLoadEntity(const QString& file_name, io::EntityIOHandlerP handler=io::EntityIOHandlerP()); +#if OST_IMG_ENABLED + static gfx::GfxObjP TryLoadMap(const QString& filename, io::MapIOHandlerPtr handler=io::MapIOHandlerPtr()) throw(io::IOException); +#endif + static gfx::GfxObjP TryLoadSurface(const QString& filename, io::SurfaceIOHandlerPtr handler=io::SurfaceIOHandlerPtr()); static void RunScript(const QString& filename); static void LoadPDB(const QString& filename); + static gfx::GfxObjP NoHandlerFound(const QString& filename); virtual ~FileLoader(); static LoaderManagerPtr loader_manager_; diff --git a/modules/gui/src/file_type_dialog.cc b/modules/gui/src/file_type_dialog.cc new file mode 100644 index 0000000000000000000000000000000000000000..ed41381cd6f9769aed80a8943d55d267fde00068 --- /dev/null +++ b/modules/gui/src/file_type_dialog.cc @@ -0,0 +1,141 @@ +//------------------------------------------------------------------------------ +// This file is part of the OpenStructure project <www.openstructure.org> +// +// Copyright (C) 2008-2010 by the OpenStructure authors +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License as published by the Free +// Software Foundation; either version 3.0 of the License, or (at your option) +// any later version. +// This library 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 Lesser General Public License for more +// details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this library; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +//------------------------------------------------------------------------------ +#include "file_type_dialog.hh" + +#include <QPushButton> +#include <QVBoxLayout> +#include <QHBoxLayout> +#include <QTableWidgetItem> +#include <QHeaderView> + +#include <iostream> + +#include <ost/io/io_manager.hh> +#include <ost/io/io_exception.hh> + +namespace ost { namespace gui { + +FileTypeDialog::FileTypeDialog(const QString& file_name, QWidget* parent): + QDialog(parent),entity_handler_(), surf_handler_() +#if OST_IMG_ENABLED + ,map_handler_() +#endif +{ + this->setWindowTitle("File format not recognized"); + QVBoxLayout* vb=new QVBoxLayout(this); + label_ = new QLabel("The file format could not be recognized, please select the type of the file from the list:"); + list_ = new QTableWidget(this); + list_->setColumnCount(2); + list_->setColumnWidth(0, 50); + list_->setColumnWidth(1,525); + list_->verticalHeader()->setVisible(false); + list_->horizontalHeader()->setVisible(false); + list_->setSelectionBehavior(QAbstractItemView::SelectRows); + list_->setSelectionMode(QAbstractItemView::SingleSelection); + QHBoxLayout* hb=new QHBoxLayout(); + vb->addWidget(label_); + vb->addWidget(list_); + hb->setDirection(QBoxLayout::LeftToRight); + QPushButton* cancel_btn = new QPushButton(tr("Cancel"), this); + QPushButton* load_btn = new QPushButton(tr("Load"), this); + hb->addStretch(1); + hb->addWidget(cancel_btn); + hb->addWidget(load_btn); + vb->addLayout(hb); + load_btn->setDefault(true); + connect(load_btn, SIGNAL(clicked()), this, SLOT(accept())); + connect(cancel_btn, SIGNAL(clicked()), this, SLOT(reject())); + + io::EntityIOHFList entity_handler = io::IOManager::Instance().GetAvailableEntityHandler(); + for(unsigned int i = 0 ; i < entity_handler.size() ; i++){ + QVariant handler = QVariant(); + handler.setValue(entity_handler[i]); + this->AddRow(i,entity_handler[i]->GetFormatName().c_str(),entity_handler[i]->GetFormatDescription().c_str(),handler); + } + +#if OST_IMG_ENABLED + io::MapIOFList map_handler = io::IOManager::Instance().GetAvailableMapHandler(); + for(unsigned int i = 0 ; i < map_handler.size() ; i++){ + QVariant handler = QVariant(); + handler.setValue(map_handler[i]); + this->AddRow(i,map_handler[i]->GetFormatName().c_str(),map_handler[i]->GetFormatDescription().c_str(),handler); + } +#endif + io::SurfaceIOFList surf_handler = io::IOManager::Instance().GetAvailableSurfaceHandler(); + for(unsigned int i = 0 ; i < surf_handler.size() ; i++){ + QVariant handler = QVariant(); + handler.setValue(surf_handler[i]); + this->AddRow(i,surf_handler[i]->GetFormatName().c_str(),surf_handler[i]->GetFormatDescription().c_str(),handler); + } +} + +void FileTypeDialog::AddRow(int row, const QString& format_name, const QString& format_descr, QVariant& variant){ + list_->insertRow(row); + QTableWidgetItem* new_item = new QTableWidgetItem(format_name); + new_item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled); + new_item->setData(Qt::UserRole,variant); + list_->setItem(row, 0, new_item); + new_item = new QTableWidgetItem(format_descr); + new_item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled); + list_->setItem(row, 1, new_item); +} + +void FileTypeDialog::accept(){ + QList<QTableWidgetItem*> items = list_->selectedItems(); + for(int i=0; i<items.size();i++){ + if(items[i]->column()==0){ + QVariant variant = items[i]->data(Qt::UserRole); + io::EntityIOHandlerFactoryBaseP ent_handler_fac = variant.value<io::EntityIOHandlerFactoryBaseP>(); + if(ent_handler_fac){ + entity_handler_ = ent_handler_fac->Create(); + break; + } + io::SurfaceIOHandlerFactoryBasePtr surf_handler_fac = variant.value<io::SurfaceIOHandlerFactoryBasePtr>(); + if(surf_handler_fac){ + surf_handler_ = surf_handler_fac->Create(); + break; + } +#if OST_IMG_ENABLED + io::MapIOHandlerFactoryBasePtr map_handler_fac = variant.value<io::MapIOHandlerFactoryBasePtr>(); + if(map_handler_fac){ + map_handler_ = map_handler_fac->Create(); + break; + } +#endif + } + } + this->QDialog::accept(); +} + +io::EntityIOHandlerP FileTypeDialog::GetEntityHandler(){ + return entity_handler_; +} + + +io::SurfaceIOHandlerPtr FileTypeDialog::GetSurfaceHandler(){ + return surf_handler_; +} + +#if OST_IMG_ENABLED +io::MapIOHandlerPtr FileTypeDialog::GetMapHandler(){ + return map_handler_; +} +#endif + +}} //ns diff --git a/modules/gui/src/file_type_dialog.hh b/modules/gui/src/file_type_dialog.hh new file mode 100644 index 0000000000000000000000000000000000000000..3efee2c535739d0c2e068600a25cd5a9da49e388 --- /dev/null +++ b/modules/gui/src/file_type_dialog.hh @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// This file is part of the OpenStructure project <www.openstructure.org> +// +// Copyright (C) 2008-2010 by the OpenStructure authors +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License as published by the Free +// Software Foundation; either version 3.0 of the License, or (at your option) +// any later version. +// This library 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 Lesser General Public License for more +// details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this library; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +//------------------------------------------------------------------------------ +#ifndef OST_GUI_FILE_TYPE_DIALOG_HH +#define OST_GUI_FILE_TYPE_DIALOG_HH + +/* + Author: Stefan Scheuber + */ + +#include <ost/gui/module_config.hh> + + +#include <QDialog> +#include <QTableWidget> +#include <QLabel> + +#include <ost/io/entity_io_handler.hh> +#include <ost/io/surface_io_handler.hh> +#if OST_IMG_ENABLED +#include <ost/io/map_io_handler.hh> +#endif + +namespace ost { namespace gui { + +/// \brief dialog to select a file type +class DLLEXPORT_OST_GUI FileTypeDialog : public QDialog { + Q_OBJECT +public: + FileTypeDialog(const QString& file_name, QWidget* parent=NULL); + io::EntityIOHandlerP GetEntityHandler(); + io::SurfaceIOHandlerPtr GetSurfaceHandler(); +#if OST_IMG_ENABLED + io::MapIOHandlerPtr GetMapHandler(); +#endif + +public slots: + virtual void accept (); + +private: + void AddRow(int row, const QString& format_name, const QString& format_descr, QVariant& variant); + + + QTableWidget* list_; + QLabel* label_; + io::EntityIOHandlerP entity_handler_; + io::SurfaceIOHandlerPtr surf_handler_; +#if OST_IMG_ENABLED + io::MapIOHandlerPtr map_handler_; +#endif + +}; + +}} + +Q_DECLARE_METATYPE(ost::io::EntityIOHandlerFactoryBaseP); +Q_DECLARE_METATYPE(ost::io::SurfaceIOHandlerFactoryBasePtr); +#if OST_IMG_ENABLED +Q_DECLARE_METATYPE(ost::io::MapIOHandlerFactoryBasePtr); +#endif + +#endif