PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/liteidex/src/plugins/welcome/welcomebrowser.cpp

https://code.google.com/p/liteide/
C++ | 220 lines | 166 code | 29 blank | 25 comment | 32 complexity | 1fa7b039dd3ff448235e1d2609d0c2d9 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0
  1. /**************************************************************************
  2. ** This file is part of LiteIDE
  3. **
  4. ** Copyright (c) 2011-2013 LiteIDE Team. All rights reserved.
  5. **
  6. ** This library is free software; you can redistribute it and/or
  7. ** modify it under the terms of the GNU Lesser General Public
  8. ** License as published by the Free Software Foundation; either
  9. ** version 2.1 of the License, or (at your option) any later version.
  10. **
  11. ** This library is distributed in the hope that it will be useful,
  12. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ** Lesser General Public License for more details.
  15. **
  16. ** In addition, as a special exception, that plugins developed for LiteIDE,
  17. ** are allowed to remain closed sourced and can be distributed under any license .
  18. ** These rights are included in the file LGPL_EXCEPTION.txt in this package.
  19. **
  20. **************************************************************************/
  21. // Module: welcomebrowser.cpp
  22. // Creator: visualfc <visualfc@gmail.com>
  23. #include "welcomebrowser.h"
  24. #include "golangdocapi/golangdocapi.h"
  25. #include "litedoc.h"
  26. #include <QMenu>
  27. #include <QStatusBar>
  28. #include <QToolBar>
  29. #include <QDir>
  30. #include <QFileInfo>
  31. #include <QAction>
  32. #include <QFile>
  33. #include <QTextBrowser>
  34. #include <QDesktopServices>
  35. #include <QHBoxLayout>
  36. #include <QVBoxLayout>
  37. #include <QPushButton>
  38. #include <QDebug>
  39. //lite_memory_check_begin
  40. #if defined(WIN32) && defined(_MSC_VER) && defined(_DEBUG)
  41. #define _CRTDBG_MAP_ALLOC
  42. #include <stdlib.h>
  43. #include <crtdbg.h>
  44. #define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
  45. #define new DEBUG_NEW
  46. #endif
  47. //lite_memory_check_end
  48. WelcomeBrowser::WelcomeBrowser(LiteApi::IApplication *app, QObject *parent)
  49. : LiteApi::IBrowserEditor(parent),
  50. m_liteApp(app),
  51. m_extension(new Extension),
  52. m_widget(new QWidget)
  53. {
  54. m_browser = new DocumentBrowser(m_liteApp,this);
  55. m_browser->toolBar()->hide();
  56. QVBoxLayout *mainLayout = new QVBoxLayout;
  57. mainLayout->addWidget(m_browser->widget());
  58. QPushButton *newFile = new QPushButton(tr("New"));
  59. QPushButton *openFile = new QPushButton(tr("Open"));
  60. QPushButton *openFolder = new QPushButton(tr("Open Folder"));
  61. QPushButton *options = new QPushButton(tr("Options"));
  62. QHBoxLayout *layout = new QHBoxLayout;
  63. layout->addWidget(newFile);
  64. layout->addWidget(openFile);
  65. layout->addWidget(openFolder);
  66. layout->addStretch(1);
  67. layout->addWidget(options);
  68. mainLayout->addLayout(layout);
  69. m_widget->setLayout(mainLayout);
  70. connect(newFile,SIGNAL(clicked()),m_liteApp->fileManager(),SLOT(newFile()));
  71. connect(openFile,SIGNAL(clicked()),m_liteApp->fileManager(),SLOT(openFiles()));
  72. connect(openFolder,SIGNAL(clicked()),this,SLOT(openFolder()));
  73. connect(options,SIGNAL(clicked()),m_liteApp->optionManager(),SLOT(exec()));
  74. connect(m_browser,SIGNAL(requestUrl(QUrl)),this,SLOT(openUrl(QUrl)));
  75. connect(m_liteApp->fileManager(),SIGNAL(recentFilesChanged(QString)),this,SLOT(loadData()));
  76. connect(m_browser,SIGNAL(linkHovered(QUrl)),this,SLOT(highlightedUrl(QUrl)));
  77. m_browser->setSearchPaths(QStringList() << m_liteApp->resourcePath()+"/welcome");
  78. m_extension->addObject("LiteApi.QTextBrowser",m_browser->htmlWidget()->widget());
  79. QString path = m_liteApp->resourcePath()+"/welcome/welcome.html";
  80. QFile file(path);
  81. if (file.open(QIODevice::ReadOnly)) {
  82. m_templateData = file.readAll();
  83. file.close();
  84. }
  85. loadData();
  86. }
  87. WelcomeBrowser::~WelcomeBrowser()
  88. {
  89. delete m_browser;
  90. delete m_widget;
  91. if (m_extension) {
  92. delete m_extension;
  93. }
  94. }
  95. void WelcomeBrowser::openFolder()
  96. {
  97. m_liteApp->fileManager()->openFolder();
  98. }
  99. LiteApi::IExtension *WelcomeBrowser::extension()
  100. {
  101. return m_extension;
  102. }
  103. void WelcomeBrowser::highlightedUrl(const QUrl &url)
  104. {
  105. m_liteApp->mainWindow()->statusBar()->showMessage(url.toString());
  106. }
  107. void WelcomeBrowser::openUrl(const QUrl &url)
  108. {
  109. m_liteApp->mainWindow()->statusBar()->clearMessage();
  110. if (url.scheme() == "http" ||
  111. url.scheme() == "https" ||
  112. url.scheme() == "mailto") {
  113. QDesktopServices::openUrl(url);
  114. } else if (url.scheme() == "session") {
  115. m_liteApp->loadSession(url.path());
  116. } else if (url.scheme() == "proj") {
  117. m_liteApp->fileManager()->openProject(url.path());
  118. } else if (url.scheme() == "file") {
  119. m_liteApp->fileManager()->openEditor(url.path());
  120. } else if (url.scheme() == "folder") {
  121. m_liteApp->fileManager()->openFolderProject(url.path());
  122. }else if (url.scheme() == "doc") {
  123. LiteApi::ILiteDoc *doc = LiteApi::findExtensionObject<LiteApi::ILiteDoc*>(m_liteApp,"LiteApi.ILiteDoc");
  124. if (doc) {
  125. doc->openUrl(url.path());
  126. doc->activeBrowser();
  127. }
  128. } else if (url.scheme() == "godoc") {
  129. LiteApi::IGolangDoc *doc = LiteApi::findExtensionObject<LiteApi::IGolangDoc*>(m_liteApp,"LiteApi.IGolangDoc");
  130. if (doc) {
  131. doc->openUrl(url.path());
  132. doc->activeBrowser();
  133. }
  134. } else if (url.scheme() == "goplay") {
  135. LiteApi::IEditor *browser = LiteApi::findExtensionObject<LiteApi::IEditor*>(m_liteApp,"LiteApi.Goplay");
  136. if (browser) {
  137. m_liteApp->editorManager()->activeBrowser(browser);
  138. }
  139. }
  140. }
  141. void WelcomeBrowser::loadData()
  142. {
  143. QString data = m_templateData;
  144. QStringList sessionList;
  145. sessionList.append("<ul>");
  146. sessionList.append(QString("<li><a href=\"session:default\">default</a></li>"));
  147. sessionList.append("</ul>");
  148. QStringList list;
  149. QStringList schemeList;
  150. //schemeList = m_liteApp->fileManager()->schemeList();
  151. schemeList << "folder" << "file";
  152. schemeList.append(m_liteApp->fileManager()->schemeList());
  153. schemeList.removeDuplicates();
  154. foreach (QString scheme, schemeList) {
  155. QString s = scheme.left(1).toUpper()+scheme.right(scheme.length()-1);
  156. list.append(QString("<h3><i>Recent %1</i></h3>").arg(s));
  157. list.append("<table border=\"0\"><tr><td>");
  158. list.append("<ul>");
  159. QStringList recentProjects = m_liteApp->fileManager()->recentFiles(scheme);
  160. int count = 0;
  161. foreach (QString file, recentProjects) {
  162. QFileInfo info(file);
  163. list.append(QString("<li><a href=\"%1:%2\">%3</a> <span class=\"recent\">%4</span></li>")
  164. .arg(scheme)
  165. .arg(info.filePath())
  166. .arg(info.fileName())
  167. .arg(QDir::toNativeSeparators(info.filePath())));
  168. if (count++ > 8) {
  169. break;
  170. }
  171. }
  172. list.append("</ul>");
  173. list.append("</td></tr></table>");
  174. }
  175. data.replace("{recent_sessions}",sessionList.join("\n"));
  176. data.replace("{recent_files}",list.join("\n"));
  177. QUrl url(m_liteApp->resourcePath()+"/welcome/welcome.html");
  178. m_browser->setUrlHtml(url,data);
  179. }
  180. QWidget *WelcomeBrowser::widget()
  181. {
  182. return m_widget;
  183. }
  184. QString WelcomeBrowser::name() const
  185. {
  186. return tr("Welcome");
  187. }
  188. QString WelcomeBrowser::mimeType() const
  189. {
  190. return "browser/welcome";
  191. }