OpenWalnut  1.5.0dev
WQtModuleMetaInfo.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <string>
26 #include <vector>
27 #include <iostream>
28 
29 #ifdef OW_FORCE_WEBKIT
30  #include <QWebView>
31  #include <QWebFrame>
32  #include <QWebPage>
33 #else
34  #include <QWebEngineView> //NOLINT
35  #include <QWebEnginePage> //NOLINT
36 #endif
37 #include <QVBoxLayout>
38 #include <QToolBar>
39 #include <QToolButton>
40 #include <QHBoxLayout>
41 #include <QWidget>
42 #include <QPixmap>
43 #include <QIcon>
44 #include <QAction>
45 #include <QtCore/QUrl>
46 #include <QTextEdit>
47 
48 #include "core/common/WIOTools.h"
49 #include "core/common/WLogger.h"
50 #include "core/common/WPathHelper.h"
51 
52 #include "../WQtGui.h"
53 #include "../WMainWindow.h"
54 
55 #include "WQtModuleMetaInfo.h"
56 
57 std::string htmlify( WModuleMetaInformation::ConstSPtr meta )
58 {
59  typedef std::vector< WModuleMetaInformation::Author > AuthorType;
60  AuthorType authors = meta->getAuthors();
61  typedef std::vector< std::string > TagType;
62  TagType tags = meta->getTags();
63  boost::filesystem::path help = meta->getHelp();
64  std::string website = meta->getWebsite();
65  std::string description = meta->getDescription();
66  typedef std::vector< WModuleMetaInformation::Online > OnlineType;
67  OnlineType online = meta->getOnlineResources();
68  typedef std::vector< WModuleMetaInformation::Screenshot > ScreenshotType;
69  ScreenshotType screenshots = meta->getScreenshots();
70 
71  // where to put in the html code
72  std::ostringstream ss;
73 
74  // the website URL is the module dir -> so we need to explicitly specify the src for icons
75  std::string iconPath = WPathHelper::getSharePath().string() + "/qtgui/";
76 
77  ss << "<div style='font-family:sans-serif;'>"
78  // The title
79  << " <div style='padding:10px; border:0px solid #000; border-bottom-width: 10px;color:#fff;background:#0c67a8;'>"
80  << " <h1>"
81  << meta->getName()
82  << " </h1>"
83  << " </div>";
84 
85  // Description
86  ss << " <div style='padding:10px; background:#ddd;'>"
87  << " <h3>DESCRIPTION</h3>"
88  << " <p align='justify'>"
89  << description
90  << " </p>"
91  << " </div>";
92 
93  // this div contains tags, help, website and authors
94  ss << " <div style='padding:10px;background:#f3f3f3;'>";
95  // Taglist
96  if( !tags.empty() )
97  {
98  ss << " <h3>TAGS</h3>";
99  for( TagType::const_iterator iter = tags.begin(); iter != tags.end(); ++iter )
100  {
101  ss << *iter;
102  if( iter+1 != tags.end() )
103  {
104  ss << ", ";
105  }
106  }
107  }
108 
109  // Website
110  if( !website.empty() )
111  {
112  ss << " <h3>WEBSITE</h3>"
113  << " <a href='" << website << "'>" << website << "</a>";
114  }
115 
116  // Help
117  if( !help.empty() && boost::filesystem::exists( help ) )
118  {
119  ss << " <h3>HELP</h3>"
120  << " <div>" << readFileIntoString( help ) << "</div>";
121  }
122  // The authors
123  if( !authors.empty() )
124  {
125  ss << " <h3>AUTHORS</h3>"
126  << " <table width='100%' style='padding:10px'>";
127  for( AuthorType::const_iterator iter = authors.begin(); iter != authors.end(); ++iter )
128  {
129  ss << "<tr> <td style='padding:10px; background:#ddd;'>";
130 
131  if( !( *iter ).m_email.empty() )
132  {
133  ss << "<a href='mailto:" << ( *iter ).m_email << "'>" << "<img width='24px' src='" << iconPath << "/email.png' />" << "</a>";
134  }
135  if( !( *iter ).m_url.empty() )
136  {
137  ss << "<a href='" << ( *iter ).m_url << "'>" << "<img width='24px' src='" << iconPath << "website.png' />" << "</a>";
138  }
139 
140  ss << "&nbsp;";
141  ss << ( *iter ).m_name;
142  ss << "</td><td style='padding:10px; background:#e7e7e7;'><p align='justify'>" << ( *iter ).m_what << "</p></td>";
143  ss << "</tr>";
144  }
145  ss << " </table>";
146  }
147 
148  // thats it. No more authors, help and similar
149  ss << " </div>";
150 
151  // The online resources
152  if( !online.empty() )
153  {
154  ss << " <div style='padding:10px;background:#fff;'>"
155  << " <h3>ONLINE RESOURCES</h3>"
156  << " <table width='100%' style='padding:10px'>";
157 
158  for( OnlineType::const_iterator iter = online.begin(); iter != online.end(); ++iter )
159  {
160  ss << " <tr>"
161  << " <td style='padding:10px; background:#ddd;'>";
162 
163  ss << " <a href='" << ( *iter ).m_url << "'> " << ( *iter ).m_name << "</a>";
164 
165  ss << " </td>"
166  << " <td style='padding:10px; background:#e7e7e7;'>";
167 
168  ss << " <p align='justify'>" << ( *iter ).m_description << "</p>";
169 
170  ss << " </td>"
171  << " </tr>";
172  }
173 
174  ss << " </table>"
175  << " </div>";
176  }
177 
178  // screenshots
179  if( !screenshots.empty() )
180  {
181  ss << " <div style='padding:10px;background:#f5f5f5;'>"
182  << " <h3>SCREENSHOTS</h3>"
183  << " <table width='100%' style='padding:10px'>";
184 
185  for( ScreenshotType::const_iterator iter = screenshots.begin(); iter != screenshots.end(); ++iter )
186  {
187  ss << " <tr>"
188  << " <td width='1%' style='padding:10px; background:#ddd;'>";
189 
190  ss << " <img src='" << ( *iter ).m_filename.string() << "' width=200px>";
191 
192  ss << " </td>"
193  << " <td style='padding:10px; background:#e7e7e7;'>";
194 
195  ss << " <p align='justify'>" << ( *iter ).m_description << "</p>";
196 
197  ss << " </td>"
198  << " </tr>";
199  }
200 
201  ss << " </table>"
202  << " </div>"
203  << "</div>";
204  }
205  return ss.str();
206 }
207 
209  QWidget( parent ),
210  m_module( module )
211 {
212  // some layout to place the webview
213  QVBoxLayout *layout = new QVBoxLayout();
214  setLayout( layout );
215  layout->setSpacing( 0 );
216  layout->setContentsMargins( 0, 0, 0, 0 );
217 
218 #ifdef OW_FORCE_WEBKIT
219  // create the QT webview
220  QWebView* view = new QWebView( this );
221 
222  // create a webpage and add it to the view
223  QWebPage* page = new QWebPage( this );
224  page->setLinkDelegationPolicy( QWebPage::DelegateExternalLinks );
225  m_frame = page->mainFrame();
226 #else
227  // create the QT webview
228  QWebEngineView* view = new QWebEngineView( this );
229 
230  // create a webpage and add it to the view
231  QWebEnginePage* page = new QWebEnginePage( this );
232  m_page = page;
233 #endif
234  view->setPage( page );
235 
236  // add a toolbar for basic navigation
237  QWidget* toolbar = new QWidget( this );
238  QHBoxLayout* tbLayout = new QHBoxLayout( toolbar );
239  toolbar->setLayout( tbLayout );
240 
241  // we need a special home-action to set the html content again
242  QAction* homeAction = new QAction( WQtGui::getMainWindow()->getIconManager()->getIcon( module->getName() ),
243  QString( "Back to " ) + QString::fromStdString( module->getName() ),
244  toolbar );
245  homeAction->setIconText( QString( "Back to " ) + QString::fromStdString( module->getName() ) );
246 
247  // add the buttons
248  QToolButton* homeBtn = new QToolButton( toolbar );
249  homeBtn->setDefaultAction( homeAction );
250  homeBtn->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
251 
252  QToolButton* backBtn = new QToolButton( toolbar );
253 #ifdef OW_FORCE_WEBKIT
254  backBtn->setDefaultAction( page->action( QWebPage::Back ) );
255 #else
256  backBtn->setDefaultAction( page->action( QWebEnginePage::Back ) );
257 #endif
258  QToolButton* fwdBtn = new QToolButton( toolbar );
259 #ifdef OW_FORCE_WEBKIT
260  fwdBtn->setDefaultAction( page->action( QWebPage::Forward ) );
261 #else
262  fwdBtn->setDefaultAction( page->action( QWebEnginePage::Forward ) );
263 #endif
264 
265  tbLayout->addWidget( backBtn );
266  tbLayout->addWidget( fwdBtn );
267  tbLayout->addStretch( 100000 );
268  tbLayout->addWidget( homeBtn );
269 
270  // build widget
271  layout->addWidget( toolbar );
272  layout->addWidget( view );
273 
274  layout->setStretchFactor( toolbar, 0 );
275  layout->setStretchFactor( view, 1 );
276 
277  resetContent();
278 
279  // the home action triggers reseContent
280  connect( homeAction, SIGNAL( triggered() ), this, SLOT( resetContent() ) );
281 }
282 
284 {
285  // cleanup
286 }
287 
289 {
290  // we use the module resource path as search URL
291  std::string moduleLocation( m_module->getLocalPath().string() );
292  QString locationURL( QString( "file://" ) + QString::fromStdString( moduleLocation ) + "/" );
293 
294  // set content
295  std::string processedContent = htmlify( m_module->getMetaInformation() );
296 #ifdef OW_FORCE_WEBKIT
297  m_frame->setHtml( processedContent.c_str(), QUrl( locationURL ) );
298 #else
299  m_page->setHtml( processedContent.c_str(), QUrl( locationURL ) );
300 #endif
301 }
std::shared_ptr< const WModuleMetaInformation > ConstSPtr
Convenience typedef for a std::shared_ptr< const WModuleMetaInformation >.
std::shared_ptr< WModule > SPtr
Shared pointer to a WModule.
Definition: WModule.h:106
static boost::filesystem::path getSharePath()
The path where shared files reside in.
static WMainWindow * getMainWindow()
Returns the current main window instance or NULL if not existent.
Definition: WQtGui.cpp:88
WQtModuleMetaInfo(WModule::SPtr module, QWidget *parent=0)
Default constructor.
WModule::SPtr m_module
The module.
QWebEnginePage * m_page
The frame containing the module meta info.
virtual ~WQtModuleMetaInfo()
Destructor.
void resetContent()
Resets the webview and provides the module info again.