PageRenderTime 71ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/qtbase/examples/xml/saxbookmarks/mainwindow.cpp

https://bitbucket.org/lotiuss/qt-5.11.0
C++ | 177 lines | 106 code | 22 blank | 49 comment | 14 complexity | 676870200ea6f4f069714e254f0e6c0b MD5 | raw file
Possible License(s): GPL-3.0, MPL-2.0, CC-BY-SA-3.0, LGPL-2.0, Unlicense, BSD-3-Clause, Apache-2.0, LGPL-3.0, MIT, WTFPL, GPL-2.0, MPL-2.0-no-copyleft-exception, AGPL-3.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, ISC
  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 "xbelgenerator.h"
  53. #include "xbelhandler.h"
  54. MainWindow::MainWindow()
  55. {
  56. QStringList labels;
  57. labels << tr("Title") << tr("Location");
  58. treeWidget = new QTreeWidget;
  59. treeWidget->header()->setSectionResizeMode(QHeaderView::Stretch);
  60. treeWidget->setHeaderLabels(labels);
  61. #if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
  62. treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
  63. connect(treeWidget, &QWidget::customContextMenuRequested,
  64. this, &MainWindow::onCustomContextMenuRequested);
  65. #endif
  66. setCentralWidget(treeWidget);
  67. createMenus();
  68. statusBar()->showMessage(tr("Ready"));
  69. setWindowTitle(tr("SAX Bookmarks"));
  70. const QSize availableSize = QApplication::desktop()->availableGeometry(this).size();
  71. resize(availableSize.width() / 2, availableSize.height() / 3);
  72. }
  73. #if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
  74. void MainWindow::onCustomContextMenuRequested(const QPoint &pos)
  75. {
  76. const QTreeWidgetItem *item = treeWidget->itemAt(pos);
  77. if (!item)
  78. return;
  79. const QString url = item->text(1);
  80. QMenu contextMenu;
  81. QAction *copyAction = contextMenu.addAction(tr("Copy Link to Clipboard"));
  82. QAction *openAction = contextMenu.addAction(tr("Open"));
  83. QAction *action = contextMenu.exec(treeWidget->viewport()->mapToGlobal(pos));
  84. if (action == copyAction)
  85. QGuiApplication::clipboard()->setText(url);
  86. else if (action == openAction)
  87. QDesktopServices::openUrl(QUrl(url));
  88. }
  89. #endif // !QT_NO_CONTEXTMENU && !QT_NO_CLIPBOARD
  90. void MainWindow::open()
  91. {
  92. QString fileName =
  93. QFileDialog::getOpenFileName(this, tr("Open Bookmark File"),
  94. QDir::currentPath(),
  95. tr("XBEL Files (*.xbel *.xml)"));
  96. if (fileName.isEmpty())
  97. return;
  98. treeWidget->clear();
  99. XbelHandler handler(treeWidget);
  100. QXmlSimpleReader reader;
  101. reader.setContentHandler(&handler);
  102. reader.setErrorHandler(&handler);
  103. QFile file(fileName);
  104. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  105. QMessageBox::warning(this, tr("SAX Bookmarks"),
  106. tr("Cannot read file %1:\n%2.")
  107. .arg(QDir::toNativeSeparators(fileName),
  108. file.errorString()));
  109. return;
  110. }
  111. QXmlInputSource xmlInputSource(&file);
  112. if (reader.parse(xmlInputSource))
  113. statusBar()->showMessage(tr("File loaded"), 2000);
  114. }
  115. void MainWindow::saveAs()
  116. {
  117. QString fileName =
  118. QFileDialog::getSaveFileName(this, tr("Save Bookmark File"),
  119. QDir::currentPath(),
  120. tr("XBEL Files (*.xbel *.xml)"));
  121. if (fileName.isEmpty())
  122. return;
  123. QFile file(fileName);
  124. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  125. QMessageBox::warning(this, tr("SAX Bookmarks"),
  126. tr("Cannot write file %1:\n%2.")
  127. .arg(QDir::toNativeSeparators(fileName),
  128. file.errorString()));
  129. return;
  130. }
  131. XbelGenerator generator(treeWidget);
  132. if (generator.write(&file))
  133. statusBar()->showMessage(tr("File saved"), 2000);
  134. }
  135. void MainWindow::about()
  136. {
  137. QMessageBox::about(this, tr("About SAX Bookmarks"),
  138. tr("The <b>SAX Bookmarks</b> example demonstrates how to use Qt's "
  139. "SAX classes to read XML documents and how to generate XML by "
  140. "hand."));
  141. }
  142. void MainWindow::createMenus()
  143. {
  144. QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
  145. QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &MainWindow::open);
  146. openAct->setShortcuts(QKeySequence::Open);
  147. QAction *saveAsAct = fileMenu->addAction(tr("&Save As..."), this, &MainWindow::saveAs);
  148. saveAsAct->setShortcuts(QKeySequence::SaveAs);
  149. QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close);
  150. exitAct->setShortcuts(QKeySequence::Quit);
  151. menuBar()->addSeparator();
  152. QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
  153. helpMenu->addAction(tr("&About"), this, &MainWindow::about);
  154. helpMenu->addAction(tr("About &Qt"), qApp, &QCoreApplication::quit);
  155. }