From 544415ffa9ecb8428bd2b05bb695db324529cb1d Mon Sep 17 00:00:00 2001 From: stefan <stefan@5a81b35b-ba03-0410-adc8-b2c5c5119f08> Date: Tue, 15 Jun 2010 08:44:09 +0000 Subject: [PATCH] Added FileViewer, open py files with FileViewer in FileBrowser git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2399 5a81b35b-ba03-0410-adc8-b2c5c5119f08 --- modules/gui/pymod/CMakeLists.txt | 1 + modules/gui/pymod/export_file_viewer.cc | 40 ++++++++++++++++++ modules/gui/pymod/wrap_gui.cc | 2 + modules/gui/src/CMakeLists.txt | 3 ++ modules/gui/src/file_browser.cc | 17 +++++++- modules/gui/src/file_browser.hh | 1 + modules/gui/src/file_viewer.cc | 56 +++++++++++++++++++++++++ modules/gui/src/file_viewer.hh | 40 ++++++++++++++++++ 8 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 modules/gui/pymod/export_file_viewer.cc create mode 100644 modules/gui/src/file_viewer.cc create mode 100644 modules/gui/src/file_viewer.hh diff --git a/modules/gui/pymod/CMakeLists.txt b/modules/gui/pymod/CMakeLists.txt index 9b1353330..016e6628c 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 000000000..57dd38112 --- /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 4034dfe1c..2746c6ce7 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 651079e49..e26043125 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 c7919c7ec..ee0126439 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 3f04a0b8e..a4f9f263b 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 000000000..f9ba0153e --- /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 000000000..f3ab2ae70 --- /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 -- GitLab