diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -12,7 +12,7 @@ set(kreportexample_SRCS main.cpp window.cpp - designerwindow.cpp + DesignerWidget.cpp KReportExampleData.cpp ) add_executable(kreportexample ${kreportexample_SRCS}) diff --git a/examples/DesignerWidget.h b/examples/DesignerWidget.h new file mode 100644 --- /dev/null +++ b/examples/DesignerWidget.h @@ -0,0 +1,81 @@ +/* This file is part of the KDE project + Copyright (C) 2015 by Adam Pigg + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KREPORTDESIGNERWIDGET_H +#define KREPORTDESIGNERWIDGET_H + +#include +#include +#include +#include + +#include +#include + +class KReportDesigner; +class QDomElement; +class QMainWindow; +class QScrollArea; + +//! KReportExample designer widget +class ReportDesignerWidget : public QScrollArea +{ + Q_OBJECT + +public: + ReportDesignerWidget(QWidget *parent = nullptr); + ~ReportDesignerWidget(); + + //! Creates main toolbar for main window @a mainWindow + //! @a mainWindow is required. To be called once. + QToolBar* createMainToolBar(QMainWindow *mainWindow); + + //! Creates items toolbar for main window @a mainWindow + //! @a mainWindow is required. To be called once. + QToolBar* createItemsToolBar(QMainWindow *mainWindow); + + //! Creates a property editor dock widget for the main window @a mainWindow. + //! The widget is added to the area @a area is it is not Qt::NoDockWidgetArea. + //! @a mainWindow is required. To be called once. + QDockWidget* createPropertyEditorDockWidget(QMainWindow *mainWindow, + Qt::DockWidgetArea area = Qt::NoDockWidgetArea); + + //! @return current document + QDomElement document() const; + +Q_SIGNALS: + void designChanged(const QDomElement&); + +private Q_SLOTS: + void slotItemInserted(const QString &itemId); + void slotDesignerPropertySetChanged(); + void designDirty(); + +private: + KReportDesigner *m_reportDesigner; + KPropertySet *m_propertySet; + + QPointer m_mainToolBar; + QPointer m_itemToolBar; + + QPointer m_propertyDock; + QPointer m_propertyEditor; +}; + +#endif // KREPORTDESIGNERWIDGET_H diff --git a/examples/DesignerWidget.cpp b/examples/DesignerWidget.cpp new file mode 100644 --- /dev/null +++ b/examples/DesignerWidget.cpp @@ -0,0 +1,126 @@ +/* This file is part of the KDE project + Copyright (C) 2015 by Adam Pigg + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "DesignerWidget.h" +#include "KReportExampleData.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +ReportDesignerWidget::ReportDesignerWidget(QWidget *parent) + : QScrollArea(parent) +{ + m_reportDesigner = new KReportDesigner(this); + setWidget(m_reportDesigner); + + connect(m_reportDesigner, SIGNAL(itemInserted(QString)), + this, SLOT(slotItemInserted(QString))); + connect(m_reportDesigner, SIGNAL(propertySetChanged()), + this, SLOT(slotDesignerPropertySetChanged())); + connect(m_reportDesigner, SIGNAL(dirty()), this, SLOT(designDirty())); + + m_reportDesigner->setReportData(new KReportExampleData); +} + +ReportDesignerWidget::~ReportDesignerWidget() +{ +} + +QToolBar* ReportDesignerWidget::createMainToolBar(QMainWindow *mainWindow) +{ + Q_ASSERT(mainWindow); + if (!m_mainToolBar) { + m_mainToolBar = mainWindow->addToolBar(tr("Main")); + m_mainToolBar->setObjectName("MainToolBar"); // needed by QMainWindow::saveState() + QList designerActions = m_reportDesigner->designerActions(); + foreach(QAction* action, designerActions) { + m_mainToolBar->addAction(action); + } + } + return m_mainToolBar; +} + +QToolBar* ReportDesignerWidget::createItemsToolBar(QMainWindow *mainWindow) +{ + if (!m_itemToolBar) { + m_itemToolBar = new QToolBar(tr("Items")); + mainWindow->addToolBar(Qt::LeftToolBarArea, m_itemToolBar); // good position for a toolbox + m_itemToolBar->setObjectName("ItemsToolBar"); // needed by QMainWindow::saveState() + QActionGroup *group = new QActionGroup(this); + QList itemActions = KReportDesigner::itemActions(group); + foreach(QAction* action, itemActions) { + m_itemToolBar->addAction(action); + } + m_reportDesigner->plugItemActions(itemActions); + } + return m_itemToolBar; +} + +QDockWidget* ReportDesignerWidget::createPropertyEditorDockWidget(QMainWindow *mainWindow, + Qt::DockWidgetArea area) +{ + if (!m_propertyDock) { + m_propertyDock = new QDockWidget(tr("Property Editor"), mainWindow); + m_propertyDock->setObjectName("PropertyEditorDockWidget"); // needed by QMainWindow::saveState() + m_propertyEditor = new KPropertyEditorView; + m_propertyEditor->changeSet(m_reportDesigner->propertySet()); + m_propertyDock->setWidget(m_propertyEditor); + mainWindow->addDockWidget(area, m_propertyDock); + m_propertyEditor->resize(200, m_propertyEditor->height()); + slotDesignerPropertySetChanged(); + } + emit designDirty(); + return m_propertyDock; +} + +void ReportDesignerWidget::slotItemInserted(const QString &itemId) +{ + QList itemActions = m_itemToolBar->actions(); + foreach(QAction* action, itemActions) { + if (action->objectName() == itemId) { + action->setChecked(false); + } + } +} + +void ReportDesignerWidget::slotDesignerPropertySetChanged() +{ + if (m_propertyEditor) { + m_propertyEditor->changeSet(m_reportDesigner->itemPropertySet()); + } +} + +void ReportDesignerWidget::designDirty() +{ + emit designChanged(m_reportDesigner->document()); +} + +QDomElement ReportDesignerWidget::document() const +{ + return m_reportDesigner->document(); +} diff --git a/examples/designerwindow.h b/examples/designerwindow.h deleted file mode 100644 --- a/examples/designerwindow.h +++ /dev/null @@ -1,61 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2015 by Adam Pigg - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#ifndef DESIGNERWINDOW_H -#define DESIGNERWINDOW_H - -#include -#include - -#include -#include - -class QScrollArea; -class KReportDesigner; - -/*! @short KReportExample application's design window */ -class DesignerWindow : public QMainWindow -{ - Q_OBJECT - -public: - DesignerWindow(); - ~DesignerWindow(); - -Q_SIGNALS: - void designChanged(const QDomElement&); - -private Q_SLOTS: - void slotItemInserted(const QString &itemId); - void slotDesignerPropertySetChanged(); - void designDirty(); - -private: - QScrollArea * m_scrollArea; - KReportDesigner *m_reportDesigner; - KPropertySet *m_propertySet; - - QToolBar *m_mainToolbar; - QToolBar *m_itemToolbar; - - QDockWidget *m_propertyDock; - KPropertyEditorView *m_propertyEditor; -}; - -#endif // DESIGNERWINDOW_H diff --git a/examples/designerwindow.cpp b/examples/designerwindow.cpp deleted file mode 100644 --- a/examples/designerwindow.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2015 by Adam Pigg - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#include "designerwindow.h" -#include "KReportExampleData.h" - -#include - -#include -#include -#include -#include -#include - -DesignerWindow::DesignerWindow() -{ - m_scrollArea = new QScrollArea(this); - setCentralWidget(m_scrollArea); - - m_reportDesigner = new KReportDesigner(this); - m_scrollArea->setWidget(m_reportDesigner); - - m_mainToolbar = addToolBar(tr("Main")); - m_itemToolbar = addToolBar(tr("Items")); - - QList designerActions = m_reportDesigner->designerActions(); - foreach(QAction* action, designerActions) { - m_mainToolbar->addAction(action); - } - - QActionGroup *group = new QActionGroup(this); - QList itemActions = KReportDesigner::itemActions(group); - foreach(QAction* action, itemActions) { - m_itemToolbar->addAction(action); - } - - m_reportDesigner->plugItemActions(itemActions); - - connect(m_reportDesigner, SIGNAL(itemInserted(QString)), this, SLOT(slotItemInserted(QString))); - - // Set up the property editor - m_propertyDock = new QDockWidget(tr("Property Editor"), this); - m_propertyEditor = new KPropertyEditorView(this); - m_propertyDock->setWidget(m_propertyEditor); - - addDockWidget(Qt::RightDockWidgetArea, m_propertyDock); - m_propertyEditor->changeSet(m_reportDesigner->propertySet()); - - connect(m_reportDesigner, SIGNAL(propertySetChanged()), - this, SLOT(slotDesignerPropertySetChanged())); - - connect(m_reportDesigner, SIGNAL(dirty()), this, SLOT(designDirty())); - - m_reportDesigner->setReportData(new KReportExampleData()); -} - -DesignerWindow::~DesignerWindow() -{ -} - -void DesignerWindow::slotItemInserted(const QString &itemId) -{ - QList itemActions = m_itemToolbar->actions(); - foreach(QAction* action, itemActions) { - if (action->objectName() == itemId) { - action->setChecked(false); - } - } -} - -void DesignerWindow::slotDesignerPropertySetChanged() -{ - m_propertyEditor->changeSet(m_reportDesigner->itemPropertySet()); -} - -void DesignerWindow::designDirty() -{ - emit designChanged(m_reportDesigner->document()); -} - diff --git a/examples/main.cpp b/examples/main.cpp --- a/examples/main.cpp +++ b/examples/main.cpp @@ -20,7 +20,6 @@ #include #include "window.h" -#include "designerwindow.h" //static const char description[] = "An example application for the KReport library"; static const char version[] = "0.2"; @@ -35,9 +34,5 @@ Window window; window.show(); - DesignerWindow designerWindow; - designerWindow.show(); - - QObject::connect(&designerWindow, SIGNAL(designChanged(QDomElement)), &window, SLOT(showDesign(QDomElement))); return app.exec(); } diff --git a/examples/window.h b/examples/window.h --- a/examples/window.h +++ b/examples/window.h @@ -30,21 +30,27 @@ #include #include +class ReportDesignerWidget; + /*! @short KReportExample application's main window */ class Window : public QMainWindow { Q_OBJECT public: - Window(); + Window(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); virtual ~Window(); public Q_SLOTS: void showDesign(const QDomElement &design); +protected: + void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE; + private: void createMenus(); bool loadDocument(); + ReportDesignerWidget *m_designerWidget; QMenu *m_fileMenu; QAction *m_exitAction; diff --git a/examples/window.cpp b/examples/window.cpp --- a/examples/window.cpp +++ b/examples/window.cpp @@ -18,24 +18,36 @@ */ #include "window.h" +#include "DesignerWidget.h" + #include +#include +#include #include -#include #include +#include #include #include +#include +#include -#include -#include - -Window::Window() - : QMainWindow() +Window::Window(QWidget *parent, Qt::WindowFlags flags) + : QMainWindow(parent, flags) { + QSplitter *centralWidget = new QSplitter(Qt::Vertical, this); + m_designerWidget = new ReportDesignerWidget(centralWidget); + m_designerWidget->createMainToolBar(this); + m_designerWidget->createItemsToolBar(this); + m_designerWidget->createPropertyEditorDockWidget(this, Qt::RightDockWidgetArea); + connect(m_designerWidget, &ReportDesignerWidget::designChanged, + this, &Window::showDesign); + createMenus(); - m_reportView = new KReportView(this); - setCentralWidget(m_reportView); + m_reportView = new KReportView(centralWidget); + setCentralWidget(centralWidget); + centralWidget->setSizes(QList() << height() / 2 << height() / 2); #if 0 if (loadDocument()) { @@ -49,12 +61,28 @@ KReportPluginManager* manager = KReportPluginManager::self(); //! @todo Q_UNUSED(manager); + + // restore visual settings + QSettings settings("org.kde", "KReportExample"); + restoreGeometry(settings.value("MainWindow/geometry").toByteArray()); + restoreState(settings.value("MainWindow/windowState").toByteArray()); + + showDesign(m_designerWidget->document()); } Window::~Window() { } +void Window::closeEvent(QCloseEvent *event) +{ + // store visual settings + QSettings settings("org.kde", "KReportExample"); + settings.setValue("MainWindow/geometry", saveGeometry()); + settings.setValue("MainWindow/windowState", saveState()); + QMainWindow::closeEvent(event); +} + bool Window::loadDocument() { qDebug() << KREPORTEXAMPLE_DATA_DIR; diff --git a/src/renderer/KReportView.cpp b/src/renderer/KReportView.cpp --- a/src/renderer/KReportView.cpp +++ b/src/renderer/KReportView.cpp @@ -71,6 +71,7 @@ d->reportView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); QVBoxLayout *l = new QVBoxLayout; + l->setMargin(0); setLayout(l); layout()->addWidget(d->reportView);