diff --git a/modules/gui/pymod/CMakeLists.txt b/modules/gui/pymod/CMakeLists.txt index 9b1353330c31cd0ca23136381ae83aba2ac43001..016e6628cf277483e88ba76e372653d168666186 100644 --- a/modules/gui/pymod/CMakeLists.txt +++ b/modules/gui/pymod/CMakeLists.txt @@ -16,6 +16,7 @@ set(OST_GUI_PYMOD_SOURCES export_panel_bar.cc export_menu_bar.cc export_file_loader.cc + export_file_viewer.cc export_widget.cc ) set(OST_GUI_SCENE_PYMOD_MODULES diff --git a/modules/gui/pymod/export_file_viewer.cc b/modules/gui/pymod/export_file_viewer.cc new file mode 100644 index 0000000000000000000000000000000000000000..57dd381126f2d8be778b4d342b2036b7930d8de9 --- /dev/null +++ b/modules/gui/pymod/export_file_viewer.cc @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// 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 <boost/python.hpp> + +#include <ost/gui/file_viewer.hh> + +#include "sip_handler.hh" + +using namespace boost::python; +using namespace ost; +using namespace ost::gui; + + +void export_FileViewer() +{ + class_<FileViewer, boost::noncopyable >("FileViewer",init<>()) + .def(init<const QString&, optional<QWidget*> >()) + .def("Show", &FileViewer::show) + .def("Hide", &FileViewer::hide) + .def("GetQObject",&get_py_qobject<FileViewer>) + .add_property("qobject", &get_py_qobject<FileViewer>) + ; +} + diff --git a/modules/gui/pymod/wrap_gui.cc b/modules/gui/pymod/wrap_gui.cc index 4034dfe1cbead9baa952f1d6d2cacea11536191d..2746c6ce7019de29a7c0040c3e2ffc8f15dbb288 100644 --- a/modules/gui/pymod/wrap_gui.cc +++ b/modules/gui/pymod/wrap_gui.cc @@ -41,6 +41,7 @@ void export_MainArea(); void export_MenuBar(); void export_RemoteSiteLoader(); void export_FileLoader(); +void export_FileViewer(); void export_Widget(); #if OST_SPNAV_ENABLED @@ -118,6 +119,7 @@ BOOST_PYTHON_MODULE(_gui) export_SequenceViewer(); export_RemoteSiteLoader(); export_FileLoader(); + export_FileViewer(); export_Widget(); #if OST_SPNAV_ENABLED diff --git a/modules/gui/src/CMakeLists.txt b/modules/gui/src/CMakeLists.txt index 651079e4929f28e3b0cc73d7ec0dc39794830270..e26043125535f045a95cddd5eb64eef31dd978e6 100644 --- a/modules/gui/src/CMakeLists.txt +++ b/modules/gui/src/CMakeLists.txt @@ -164,6 +164,7 @@ set(OST_GUI_HEADERS change_process_name.hh file_browser.hh file_type_dialog.hh +file_viewer.hh remote_loader.hh gl_canvas.hh gl_win.hh @@ -197,6 +198,7 @@ perspective.cc widget_registry.cc file_browser.cc file_type_dialog.cc +file_viewer.cc remote_loader.cc main.cc mdi_sub_window.cc @@ -326,6 +328,7 @@ scene_win/scene_win_model.cc set(HEADERS_TO_BE_MOCCED file_browser.hh file_type_dialog.hh +file_viewer.hh remote_loader.hh gl_canvas.hh gl_win.hh diff --git a/modules/gui/src/file_browser.cc b/modules/gui/src/file_browser.cc index c7919c7ecd28f309fe625a0b19855e90774d383d..ee0126439d98103a3efc18f2baa8c0c6c09d6965 100644 --- a/modules/gui/src/file_browser.cc +++ b/modules/gui/src/file_browser.cc @@ -41,6 +41,7 @@ #include <ost/gui/perspective.hh> #include <ost/gui/panel_bar/panels.hh> #include <ost/gui/file_loader.hh> +#include <ost/gui/file_viewer.hh> #include <ost/gui/python_shell/python_interpreter.hh> @@ -226,8 +227,14 @@ void FileBrowser::ShowContextMenu(const QPoint& pos){ connect(load_action,SIGNAL(triggered(bool)),this,SLOT(LoadCurrentObject())); menu->addAction(load_action); QAction* system_open_action = new QAction(menu); - system_open_action->setText("Open with system editor"); - connect(system_open_action,SIGNAL(triggered(bool)),this,SLOT(LoadWithSystemEditor())); + if(model_->filePath(index).endsWith(".py")){ + system_open_action->setText("Show source"); + connect(system_open_action,SIGNAL(triggered(bool)),this,SLOT(LoadWithSourceViewer())); + } + else{ + system_open_action->setText("Open with system default"); + connect(system_open_action,SIGNAL(triggered(bool)),this,SLOT(LoadWithSystemEditor())); + } menu->addAction(system_open_action); } if(menu->actions().size()>0){ @@ -246,6 +253,12 @@ void FileBrowser::LoadWithSystemEditor(){ QDesktopServices::openUrl(QUrl::fromLocalFile(file_name)); } +void FileBrowser::LoadWithSourceViewer(){ + QModelIndex index = view_->selectionModel()->currentIndex(); + QString file_name=model_->filePath(index); + FileViewer* file_viewer = new FileViewer(file_name); + file_viewer->show(); +} OST_REGISTER_WIDGET_WITH_DEFAULT_FACTORY(ost::gui, FileBrowser, "File Browser"); diff --git a/modules/gui/src/file_browser.hh b/modules/gui/src/file_browser.hh index 3f04a0b8e4122c7267c44dfb7700a027e5270be0..a4f9f263bc098ce3f8a5d4ec6012b42ac9781379 100644 --- a/modules/gui/src/file_browser.hh +++ b/modules/gui/src/file_browser.hh @@ -54,6 +54,7 @@ private slots: void ShowContextMenu(const QPoint& pos); void LoadCurrentObject(); void LoadWithSystemEditor(); + void LoadWithSourceViewer(); private: void LoadObject(const QModelIndex& index); void UpdateMenu(const QString& path); diff --git a/modules/gui/src/file_viewer.cc b/modules/gui/src/file_viewer.cc new file mode 100644 index 0000000000000000000000000000000000000000..f9ba0153e94d5b4c6b0f28259b5c7c8a2db52fd5 --- /dev/null +++ b/modules/gui/src/file_viewer.cc @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// 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 <QtGui> + +#include <ost/gui/python_shell/python_syntax_highlighter.hh> + +#include "file_viewer.hh" + +namespace ost { namespace gui { + +FileViewer::FileViewer(const QString& path, QWidget* parent): + QWidget(parent) +{ + QLayout* layout = new QVBoxLayout(this); + layout->setContentsMargins(0,0,0,0); + text_viewer_ = new QTextEdit(this); + text_viewer_->setReadOnly(true); + + QFile file(path); + QTextDocument* doc = text_viewer_->document(); + highlighter_ = new PythonSyntaxHighlighter(doc); + if (file.open(QFile::ReadOnly | QFile::Text)) + text_viewer_->setText(file.readAll()); + if(doc->blockCount()>1 && path.endsWith(".py")){ + for(int i= 0; i< doc->lineCount();i++){ + QTextBlock block = doc->findBlockByLineNumber(i); + block.setUserState(BLOCKTYPE_CODE); + } + QTextBlock start = doc->findBlockByLineNumber(0); + QTextBlock end = doc->findBlockByLineNumber(doc->lineCount()-1); + doc->markContentsDirty(start.position(),end.position()+end.length()-1); + } + layout->addWidget(text_viewer_); + this->setWindowTitle(path); + this->resize(800, 800); + +} + +}} + diff --git a/modules/gui/src/file_viewer.hh b/modules/gui/src/file_viewer.hh new file mode 100644 index 0000000000000000000000000000000000000000..f3ab2ae7085c53d341838d8a8606613a5b396d18 --- /dev/null +++ b/modules/gui/src/file_viewer.hh @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// 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_VIEWER_HH +#define OST_GUI_FILE_VIEWER_HH + +#include <QMainWindow> +#include <QSyntaxHighlighter> +#include <QTextEdit> + +namespace ost { namespace gui { + +/// \brief a simplistic file viewer +class FileViewer : public QWidget { + Q_OBJECT +public: + FileViewer(const QString& path=QString(), QWidget* parent=NULL); + + QTextEdit* text_viewer_; + QSyntaxHighlighter* highlighter_; +}; + +}} + +#endif