PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/tools/publisher/mainwindow.cpp

https://github.com/qtplatz/qtplatz
C++ | 249 lines | 176 code | 50 blank | 23 comment | 21 complexity | 37150c197d09cc4c79b4b16fa9c7e34f MD5 | raw file
  1. /**************************************************************************
  2. ** Copyright (C) 2010-2014 Toshinobu Hondo, Ph.D.
  3. ** Copyright (C) 2013-2014 MS-Cheminformatics LLC, Toin, Mie Japan
  4. *
  5. ** Contact: toshi.hondo@qtplatz.com
  6. **
  7. ** Commercial Usage
  8. **
  9. ** Licensees holding valid MS-Cheminfomatics commercial licenses may use this file in
  10. ** accordance with the MS-Cheminformatics Commercial License Agreement provided with
  11. ** the Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and MS-Cheminformatics.
  13. **
  14. ** GNU Lesser General Public License Usage
  15. **
  16. ** Alternatively, this file may be used under the terms of the GNU Lesser
  17. ** General Public License version 2.1 as published by the Free Software
  18. ** Foundation and appearing in the file LICENSE.TXT included in the
  19. ** packaging of this file. Please review the following information to
  20. ** ensure the GNU Lesser General Public License version 2.1 requirements
  21. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  22. **
  23. **************************************************************************/
  24. #include "mainwindow.hpp"
  25. #include "ui_mainwindow.h"
  26. #include <QCoreApplication>
  27. #include <QDesktopServices>
  28. #include <QFileDialog>
  29. #include <QSettings>
  30. #include <QToolButton>
  31. #include <adpublisher/document.hpp>
  32. #include <adpublisher/doceditor.hpp>
  33. #include <boost/filesystem.hpp>
  34. #include <boost/filesystem/fstream.hpp>
  35. #include <boost/algorithm/string.hpp>
  36. #include <sstream>
  37. #include <fstream>
  38. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
  39. , ui( new Ui::MainWindow )
  40. , docEditor_( new adpublisher::docEditor )
  41. , doc_( std::make_shared< adpublisher::document >() )
  42. {
  43. ui->setupUi( this );
  44. setCentralWidget( docEditor_.get() );
  45. setToolButtonStyle( Qt::ToolButtonFollowStyle );
  46. ui->actionOpen->setIcon( QIcon( ":/adpublisher/images/win/fileopen.png" ) );
  47. ui->actionSave_As->setIcon( QIcon( ":/adpublisher/images/win/filesave.png" ) );
  48. ui->actionApply->setIcon( QIcon( ":/publisher/run.png" ) );
  49. docEditor_->setupTextActions( ui->menuBar->addMenu( tr( "Format" ) ) );
  50. docEditor_->setupEditActions( ui->menuBar->addMenu( tr( "Edit" ) ) );
  51. if ( auto btn = new QToolButton ) {
  52. btn->setDefaultAction( ui->actionOpen );
  53. ui->mainToolBar->addWidget( btn );
  54. }
  55. if ( auto btn = new QToolButton ) {
  56. btn->setDefaultAction( ui->actionSave_As );
  57. ui->mainToolBar->addWidget( btn );
  58. }
  59. if ( auto btn = new QToolButton ) {
  60. btn->setDefaultAction( ui->actionApply );
  61. ui->mainToolBar->addWidget( btn );
  62. }
  63. ui->actionApply->setEnabled( false );
  64. }
  65. MainWindow::~MainWindow()
  66. {
  67. delete ui;
  68. }
  69. void
  70. MainWindow::onInitialUpdate( std::shared_ptr< QSettings >& settings )
  71. {
  72. settings_ = settings;
  73. std::string filename = settings->fileName().toStdString();
  74. std::vector< QString > list;
  75. getRecentFiles( "Stylesheets", "DIRS", list, "DIR" );
  76. connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::handleOpenFile );
  77. connect(ui->actionSave_As, &QAction::triggered, this, &MainWindow::handleSaveTemplateAs );
  78. connect(ui->actionApply, &QAction::triggered, this, &MainWindow::handleApplyStylesheet );
  79. }
  80. void
  81. MainWindow::addRecentFiles( const QString& group, const QString& pfx, const QString& value, const QString& key )
  82. {
  83. addRecentFiles( *settings_, group, pfx, value, key );
  84. }
  85. void
  86. MainWindow::addRecentFiles( QSettings& settings
  87. , const QString& group, const QString& pfx, const QString& value, const QString& key )
  88. {
  89. std::vector< QString > list;
  90. getRecentFiles( settings, group, pfx, list, key );
  91. boost::filesystem::path path = boost::filesystem::path( value.toStdWString() ).generic_wstring();
  92. auto it = std::remove_if( list.begin(), list.end(), [path] ( const QString& a ){ return path == a.toStdWString(); } );
  93. if ( it != list.end() )
  94. list.erase( it, list.end() );
  95. settings.beginGroup( group );
  96. settings.beginWriteArray( pfx );
  97. settings.setArrayIndex( 0 );
  98. settings.setValue( key, QString::fromStdWString( path.generic_wstring() ) );
  99. for ( size_t i = 0; i < list.size() && i < 7; ++i ) {
  100. settings.setArrayIndex( int(i + 1) );
  101. settings.setValue( key, list[ i ] );
  102. }
  103. settings.endArray();
  104. settings.endGroup();
  105. }
  106. void
  107. MainWindow::getRecentFiles( const QString& group, const QString& pfx, std::vector<QString>& list, const QString& key ) const
  108. {
  109. getRecentFiles( *settings_, group, pfx, list, key );
  110. }
  111. void
  112. MainWindow::getRecentFiles( QSettings& settings, const QString& group, const QString& pfx, std::vector<QString>& list, const QString& key )
  113. {
  114. settings.beginGroup( group );
  115. int size = settings.beginReadArray( pfx );
  116. for ( int i = 0; i < size; ++i ) {
  117. settings.setArrayIndex( i );
  118. list.push_back( settings.value( key ).toString() );
  119. }
  120. settings.endArray();
  121. settings.endGroup();
  122. }
  123. QString
  124. MainWindow::recentFile( const QString& group, const QString& pfx, const QString& key ) const
  125. {
  126. QString value;
  127. settings_->beginGroup( group );
  128. if ( int size = settings_->beginReadArray( pfx ) ) {
  129. (void)size;
  130. settings_->setArrayIndex( 0 );
  131. value = settings_->value( key ).toString();
  132. }
  133. settings_->endArray();
  134. settings_->endGroup();
  135. return value;
  136. }
  137. void
  138. MainWindow::handleOpenFile()
  139. {
  140. auto name = QFileDialog::getOpenFileName( this
  141. , tr( "Open QtPlatz publisher xml" )
  142. , recentFile( "RecentFiles", "Files" )
  143. , tr( "QtPlatz publisher xml(*.xml);;HTML(*.html)" ) );
  144. if ( !name.isEmpty() ) {
  145. boost::filesystem::path path( name.toStdWString() );
  146. if ( path.extension() == ".xml" ) {
  147. addRecentFiles( "RecentFiles", "Files", name );
  148. auto doc = std::make_shared< adpublisher::document>();
  149. if ( doc->load_file( name.toStdString().c_str() ) ) {
  150. setWindowTitle( name );
  151. xmlpath_ = name.toStdString();
  152. ui->actionApply->setEnabled( true );
  153. processed_.clear();
  154. docEditor_->setDocument( doc );
  155. }
  156. }
  157. else if ( path.extension() == ".html" ) {
  158. docEditor_->setOutput( QUrl( QString( "file:///%1" ).arg( path.string().c_str() ) ) );
  159. }
  160. }
  161. }
  162. void
  163. MainWindow::handleApplyStylesheet()
  164. {
  165. processed_.clear();
  166. QString method;
  167. QString xslpath = docEditor_->currentStylesheet();
  168. if ( adpublisher::document::apply_template( xmlpath_.c_str(), xslpath.toStdString().c_str(), processed_, method ) ) {
  169. docEditor_->setOutput( processed_, method );
  170. boost::filesystem::path path( xmlpath_ );
  171. std::string extension = method.isEmpty() ? ".html" : (QString( ".%1" ).arg( method )).toStdString();
  172. path.replace_extension( extension );
  173. boost::filesystem::ofstream o( path );
  174. o << processed_.toStdString();
  175. QUrl url( QUrl::fromLocalFile( QString::fromStdWString( path.wstring() ) ) );
  176. QDesktopServices::openUrl( url );
  177. }
  178. }
  179. void
  180. MainWindow::handleSaveProcessedAs()
  181. {
  182. if ( xmlpath_.empty() || processed_.isEmpty() )
  183. return;
  184. boost::filesystem::path outfile( xmlpath_ );
  185. outfile.replace_extension( ".html" );
  186. auto name = QFileDialog::getSaveFileName( this
  187. , tr( "Save Result" )
  188. , QString::fromStdString( outfile.string() )
  189. , tr( "HTML Files(*.html);;XML Files(*.xml)" ) );
  190. if ( !name.isEmpty() ) {
  191. std::ofstream of( name.toStdString() );
  192. of << processed_.toStdString();
  193. }
  194. }
  195. void
  196. MainWindow::handleSaveTemplateAs()
  197. {
  198. docEditor_->fileSave();
  199. }