PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/xml/streambookmarks/mainwindow.cpp

https://gitlab.com/f3822/qtbase
C++ | 189 lines | 107 code | 23 blank | 59 comment | 15 complexity | c43dc6df1652a5f6fbc71f979b407e50 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the examples of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** BSD License Usage
  18. ** Alternatively, you may use this file under the terms of the BSD license
  19. ** as follows:
  20. **
  21. ** "Redistribution and use in source and binary forms, with or without
  22. ** modification, are permitted provided that the following conditions are
  23. ** met:
  24. ** * Redistributions of source code must retain the above copyright
  25. ** notice, this list of conditions and the following disclaimer.
  26. ** * Redistributions in binary form must reproduce the above copyright
  27. ** notice, this list of conditions and the following disclaimer in
  28. ** the documentation and/or other materials provided with the
  29. ** distribution.
  30. ** * Neither the name of The Qt Company Ltd nor the names of its
  31. ** contributors may be used to endorse or promote products derived
  32. ** from this software without specific prior written permission.
  33. **
  34. **
  35. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  46. **
  47. ** $QT_END_LICENSE$
  48. **
  49. ****************************************************************************/
  50. #include <QtWidgets>
  51. #include "mainwindow.h"
  52. #include "xbelreader.h"
  53. #include "xbelwriter.h"
  54. //! [0]
  55. MainWindow::MainWindow()
  56. {
  57. QStringList labels;
  58. labels << tr("Title") << tr("Location");
  59. treeWidget = new QTreeWidget;
  60. treeWidget->header()->setSectionResizeMode(QHeaderView::Stretch);
  61. treeWidget->setHeaderLabels(labels);
  62. #if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
  63. treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
  64. connect(treeWidget, &QWidget::customContextMenuRequested,
  65. this, &MainWindow::onCustomContextMenuRequested);
  66. #endif
  67. setCentralWidget(treeWidget);
  68. createMenus();
  69. statusBar()->showMessage(tr("Ready"));
  70. setWindowTitle(tr("QXmlStream Bookmarks"));
  71. const QSize availableSize = screen()->availableGeometry().size();
  72. resize(availableSize.width() / 2, availableSize.height() / 3);
  73. }
  74. //! [0]
  75. #if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
  76. void MainWindow::onCustomContextMenuRequested(const QPoint &pos)
  77. {
  78. const QTreeWidgetItem *item = treeWidget->itemAt(pos);
  79. if (!item)
  80. return;
  81. const QString url = item->text(1);
  82. QMenu contextMenu;
  83. QAction *copyAction = contextMenu.addAction(tr("Copy Link to Clipboard"));
  84. QAction *openAction = contextMenu.addAction(tr("Open"));
  85. QAction *action = contextMenu.exec(treeWidget->viewport()->mapToGlobal(pos));
  86. if (action == copyAction)
  87. QGuiApplication::clipboard()->setText(url);
  88. else if (action == openAction)
  89. QDesktopServices::openUrl(QUrl(url));
  90. }
  91. #endif // !QT_NO_CONTEXTMENU && !QT_NO_CLIPBOARD
  92. //! [1]
  93. void MainWindow::open()
  94. {
  95. QString fileName =
  96. QFileDialog::getOpenFileName(this, tr("Open Bookmark File"),
  97. QDir::currentPath(),
  98. tr("XBEL Files (*.xbel *.xml)"));
  99. if (fileName.isEmpty())
  100. return;
  101. treeWidget->clear();
  102. QFile file(fileName);
  103. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  104. QMessageBox::warning(this, tr("QXmlStream Bookmarks"),
  105. tr("Cannot read file %1:\n%2.")
  106. .arg(QDir::toNativeSeparators(fileName),
  107. file.errorString()));
  108. return;
  109. }
  110. XbelReader reader(treeWidget);
  111. if (!reader.read(&file)) {
  112. QMessageBox::warning(this, tr("QXmlStream Bookmarks"),
  113. tr("Parse error in file %1:\n\n%2")
  114. .arg(QDir::toNativeSeparators(fileName),
  115. reader.errorString()));
  116. } else {
  117. statusBar()->showMessage(tr("File loaded"), 2000);
  118. }
  119. }
  120. //! [1]
  121. //! [2]
  122. void MainWindow::saveAs()
  123. {
  124. QString fileName =
  125. QFileDialog::getSaveFileName(this, tr("Save Bookmark File"),
  126. QDir::currentPath(),
  127. tr("XBEL Files (*.xbel *.xml)"));
  128. if (fileName.isEmpty())
  129. return;
  130. QFile file(fileName);
  131. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  132. QMessageBox::warning(this, tr("QXmlStream Bookmarks"),
  133. tr("Cannot write file %1:\n%2.")
  134. .arg(QDir::toNativeSeparators(fileName),
  135. file.errorString()));
  136. return;
  137. }
  138. XbelWriter writer(treeWidget);
  139. if (writer.writeFile(&file))
  140. statusBar()->showMessage(tr("File saved"), 2000);
  141. }
  142. //! [2]
  143. //! [3]
  144. void MainWindow::about()
  145. {
  146. QMessageBox::about(this, tr("About QXmlStream Bookmarks"),
  147. tr("The <b>QXmlStream Bookmarks</b> example demonstrates how to use Qt's "
  148. "QXmlStream classes to read and write XML documents."));
  149. }
  150. //! [3]
  151. //! [5]
  152. void MainWindow::createMenus()
  153. {
  154. QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
  155. QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &MainWindow::open);
  156. openAct->setShortcuts(QKeySequence::Open);
  157. QAction *saveAsAct = fileMenu->addAction(tr("&Save As..."), this, &MainWindow::saveAs);
  158. saveAsAct->setShortcuts(QKeySequence::SaveAs);
  159. QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close);
  160. exitAct->setShortcuts(QKeySequence::Quit);
  161. menuBar()->addSeparator();
  162. QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
  163. helpMenu->addAction(tr("&About"), this, &MainWindow::about);
  164. helpMenu->addAction(tr("About &Qt"), qApp, &QCoreApplication::quit);
  165. }
  166. //! [5]