0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-25 00:54:39 +03:00
LimeReport/limereport/objectsbrowser/lrobjectbrowser.cpp

364 lines
14 KiB
C++
Raw Permalink Normal View History

2016-02-17 10:11:00 +03:00
/***************************************************************************
* This file is part of the Lime Report project *
* Copyright (C) 2015 by Alexander Arin *
* arin_a@bk.ru *
* *
** GNU General Public License Usage **
* *
* This library is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
** GNU Lesser General Public License **
* *
* This library is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library. *
* If not, see <http://www.gnu.org/licenses/>. *
* *
* 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 General Public License for more details. *
****************************************************************************/
#include "lrobjectbrowser.h"
#include "lritemdesignintf.h"
#include "lrbanddesignintf.h"
#include <QVBoxLayout>
namespace LimeReport{
ObjectBrowser::ObjectBrowser(QWidget *parent)
2018-05-31 18:49:57 +03:00
:QWidget(parent), m_designerWidget(NULL), m_mainWindow(NULL),
2018-04-24 15:51:06 +03:00
m_changingItemSelection(false), m_movingItem(false)
2016-02-17 10:11:00 +03:00
{
QVBoxLayout *layout = new QVBoxLayout(this);
setLayout(layout);
2019-05-26 15:15:06 +03:00
layout->setMargin(Const::DOCKWIDGET_MARGINS);
2016-02-17 10:11:00 +03:00
m_treeView = new QTreeWidget(this);
layout->addWidget(m_treeView);
m_treeView->headerItem()->setText(0,tr("Objects"));
m_treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
}
2018-05-31 18:49:57 +03:00
void ObjectBrowser::setReportEditor(ReportDesignWidget *designerWidget)
2016-02-17 10:11:00 +03:00
{
2018-05-31 18:49:57 +03:00
m_designerWidget=designerWidget;
connect(m_designerWidget,SIGNAL(cleared()),this,SLOT(slotClear()));
connect(m_designerWidget, SIGNAL(loadFinished()), this, SLOT(slotReportLoaded()));
connect(m_designerWidget, SIGNAL(activePageChanged()), this, SLOT(slotActivePageChanged()));
2016-02-17 10:11:00 +03:00
2018-05-31 18:49:57 +03:00
connect(m_designerWidget,SIGNAL(itemAdded(LimeReport::PageDesignIntf*,LimeReport::BaseDesignIntf*)),
2016-02-17 10:11:00 +03:00
this, SLOT(slotItemAdded(LimeReport::PageDesignIntf*,LimeReport::BaseDesignIntf*)));
2018-05-31 18:49:57 +03:00
connect(m_designerWidget, SIGNAL(itemDeleted(LimeReport::PageDesignIntf*,LimeReport::BaseDesignIntf*)),
2016-02-17 10:11:00 +03:00
this, SLOT(slotItemDeleted(LimeReport::PageDesignIntf*,LimeReport::BaseDesignIntf*)));
2018-05-31 18:49:57 +03:00
connect(m_designerWidget, SIGNAL(bandAdded(LimeReport::PageDesignIntf*,LimeReport::BandDesignIntf*)),
2016-02-17 10:11:00 +03:00
this, SLOT(slotBandAdded(LimeReport::PageDesignIntf*,LimeReport::BandDesignIntf*)));
2018-05-31 18:49:57 +03:00
connect(m_designerWidget, SIGNAL(bandDeleted(LimeReport::PageDesignIntf*,LimeReport::BandDesignIntf*)),
2016-02-17 10:11:00 +03:00
this, SLOT(slotBandDeleted(LimeReport::PageDesignIntf*,LimeReport::BandDesignIntf*)));
connect(m_treeView, SIGNAL(itemSelectionChanged()),
this, SLOT(slotObjectTreeItemSelectionChanged()) );
2018-05-31 18:49:57 +03:00
connect(m_designerWidget, SIGNAL(itemSelected(LimeReport::BaseDesignIntf*)),
2016-02-17 10:11:00 +03:00
this, SLOT(slotItemSelected(LimeReport::BaseDesignIntf*)));
2018-05-31 18:49:57 +03:00
connect(m_designerWidget, SIGNAL(multiItemSelected()),
2016-02-17 10:11:00 +03:00
this, SLOT(slotMultiItemSelected()) );
2018-05-31 18:49:57 +03:00
connect(m_designerWidget, SIGNAL(activePageUpdated(LimeReport::PageDesignIntf*)),
this, SLOT(slotActivePageUpdated(LimeReport::PageDesignIntf*)));
2016-02-17 10:18:19 +03:00
connect(m_treeView, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
this, SLOT(slotItemDoubleClicked(QTreeWidgetItem*,int)));
2016-02-17 10:11:00 +03:00
buildTree();
}
void ObjectBrowser::setMainWindow(QMainWindow *mainWindow)
{
m_mainWindow=mainWindow;
}
void ObjectBrowser::slotClear()
{
}
void ObjectBrowser::fillNode(QTreeWidgetItem* parentNode, BaseDesignIntf* reportItem, BaseDesignIntf *ignoredItem){
2018-04-24 15:51:06 +03:00
2016-02-17 10:11:00 +03:00
foreach (BaseDesignIntf* item, reportItem->childBaseItems()) {
if (item != ignoredItem){
ObjectBrowserNode* treeItem = new ObjectBrowserNode(/*parentNode*/);
2016-02-17 10:11:00 +03:00
treeItem->setText(0,item->objectName());
treeItem->setObject(item);
treeItem->setIcon(0,QIcon(":/items/"+extractClassName(item->metaObject()->className())));
connect(item, SIGNAL(propertyObjectNameChanged(QString,QString)),
2018-04-24 15:51:06 +03:00
this, SLOT(slotPropertyObjectNameChanged(QString,QString)), Qt::UniqueConnection);
ItemDesignIntf* i = dynamic_cast<ItemDesignIntf*>(item);
if (i){
connect(i, SIGNAL(itemLocationChanged(BaseDesignIntf*,BaseDesignIntf*)),
2018-04-24 15:51:06 +03:00
this, SLOT(slotItemParentChanged(BaseDesignIntf*,BaseDesignIntf*)), Qt::UniqueConnection);
}
2016-02-17 10:11:00 +03:00
m_itemsMap.insert(item,treeItem);
BandDesignIntf* band = dynamic_cast<BandDesignIntf*>(item);
QSet<BandDesignIntf::BandsType> subBands;
subBands << BandDesignIntf::SubDetailBand <<
BandDesignIntf::SubDetailHeader <<
BandDesignIntf::SubDetailFooter;
if (band && subBands.contains(band->bandType())){
ObjectBrowserNode* parentBandNode = 0;
if (band->bandType() == BandDesignIntf::SubDetailBand){
parentBandNode = m_itemsMap.value(band->parentBand());
} else {
parentBandNode = m_itemsMap.value(band->parentBand()->parentBand());
}
if(parentBandNode)
parentBandNode->addChild(treeItem);
} else {
parentNode->addChild(treeItem);
}
2016-02-17 10:11:00 +03:00
if (!item->childBaseItems().isEmpty())
fillNode(treeItem,item, ignoredItem);
}
}
}
void ObjectBrowser::buildTree(BaseDesignIntf* ignoredItem){
m_treeView->clear();
m_itemsMap.clear();
2018-05-31 18:49:57 +03:00
if (!m_designerWidget->activePage()) return;
2016-02-17 10:11:00 +03:00
ObjectBrowserNode *topLevelItem=new ObjectBrowserNode(m_treeView);
2018-05-31 18:49:57 +03:00
topLevelItem->setText(0,m_designerWidget->activePage()->objectName());
topLevelItem->setObject(m_designerWidget->activePage());
m_itemsMap.insert(m_designerWidget->activePage(),topLevelItem);
2016-02-17 10:11:00 +03:00
m_treeView->addTopLevelItem(topLevelItem);
2018-05-31 18:49:57 +03:00
QList<QGraphicsItem*> itemsList = m_designerWidget->activePage()->items();
2016-02-17 10:39:17 +03:00
foreach (QGraphicsItem* item, itemsList) {
2016-02-17 10:11:00 +03:00
if (item != ignoredItem){
BaseDesignIntf* reportItem = dynamic_cast<BaseDesignIntf*>(item);
if (reportItem && reportItem->parentItem()==0){
ObjectBrowserNode* tItem = new ObjectBrowserNode(topLevelItem);
tItem->setText(0,reportItem->objectName());
tItem->setObject(reportItem);
tItem->setIcon(0,QIcon(":/items/"+extractClassName(reportItem->metaObject()->className())));
connect(reportItem, SIGNAL(propertyObjectNameChanged(QString,QString)),
this, SLOT(slotPropertyObjectNameChanged(QString,QString)));
m_itemsMap.insert(reportItem,tItem);
fillNode(tItem,reportItem, ignoredItem);
topLevelItem->addChild(tItem);
}
}
}
m_treeView->sortItems(0,Qt::AscendingOrder);
m_treeView->expandAll();
}
void ObjectBrowser::findAndRemove(QTreeWidgetItem* node, BaseDesignIntf* item){
for (int i=0;i<node->childCount();i++){
QTreeWidgetItem* treeItem = node->child(i);
if (treeItem->text(0)==item->objectName()){
node->removeChild(treeItem);
break;
} else {
if (treeItem->childCount()>0) findAndRemove(treeItem, item);
}
}
}
void ObjectBrowser::slotPropertyObjectNameChanged(const QString &oldName, const QString &newName)
{
Q_UNUSED(oldName)
if (m_itemsMap.contains(sender())){
m_itemsMap.value(sender())->setText(0,newName);
}
}
//void ObjectBrowser::slotObjectNameChanged(const QString &objectName)
//{
// if (m_itemsMap.contains(sender())){
// m_itemsMap.value(sender())->setText(0,objectName);
// }
//}
void ObjectBrowser::removeItem(BaseDesignIntf *item)
{
findAndRemove(m_treeView->topLevelItem(0),item);
}
void ObjectBrowser::slotReportLoaded()
{
buildTree();
}
void ObjectBrowser::slotActivePageChanged()
{
buildTree();
}
void ObjectBrowser::slotBandAdded(LimeReport::PageDesignIntf *, BandDesignIntf *)
{
buildTree();
}
void ObjectBrowser::slotBandDeleted(PageDesignIntf *, BandDesignIntf * item)
{
buildTree(item);
}
2016-02-17 10:39:17 +03:00
void ObjectBrowser::slotItemAdded(PageDesignIntf *page, BaseDesignIntf *)
2016-02-17 10:11:00 +03:00
{
2016-02-17 10:39:17 +03:00
if (!page->isUpdating()) buildTree();
2016-02-17 10:11:00 +03:00
}
void ObjectBrowser::slotItemDeleted(PageDesignIntf *, BaseDesignIntf *item)
{
if (dynamic_cast<LayoutDesignIntf*>(item)){
buildTree(item);
} else {
removeItem(item);
}
}
void ObjectBrowser::slotObjectTreeItemSelectionChanged()
{
2018-05-31 18:49:57 +03:00
if (!m_changingItemSelection && m_designerWidget->activePage()){
2016-02-17 10:11:00 +03:00
m_changingItemSelection = true;
2018-05-31 18:49:57 +03:00
m_designerWidget->activePage()->clearSelection();
2016-02-17 10:11:00 +03:00
foreach(QTreeWidgetItem* item, m_treeView->selectedItems()){
ObjectBrowserNode* tn = dynamic_cast<ObjectBrowserNode*>(item);
if (tn){
BaseDesignIntf* si = dynamic_cast<BaseDesignIntf*>(tn->object());
if (si) {
2018-05-31 18:49:57 +03:00
m_designerWidget->activePage()->animateItem(si);
2016-02-17 10:11:00 +03:00
si->setSelected(true);
2016-06-10 18:05:18 +03:00
QPointF p = si->mapToScene(si->pos());
if (si->parentItem())
p = si->parentItem()->mapToScene(si->pos());
2018-05-31 18:49:57 +03:00
m_designerWidget->activeView()->centerOn(p);
2016-02-17 10:11:00 +03:00
}
}
}
m_changingItemSelection = false;
}
}
void ObjectBrowser::slotItemSelected(LimeReport::BaseDesignIntf *item)
{
if (!m_changingItemSelection){
m_changingItemSelection = true;
m_treeView->selectionModel()->clear();
BaseDesignIntf* bg = dynamic_cast<BaseDesignIntf*>(item);
if (bg){
if (m_itemsMap.value(bg))
m_itemsMap.value(bg)->setSelected(true);
}
m_changingItemSelection = false;
}
}
void ObjectBrowser::slotMultiItemSelected()
{
2016-02-17 10:39:17 +03:00
if (!m_changingItemSelection){
m_changingItemSelection = true;
2016-02-17 10:11:00 +03:00
2016-02-17 10:39:17 +03:00
m_treeView->selectionModel()->clear();
2016-02-17 10:11:00 +03:00
2018-05-31 18:49:57 +03:00
foreach(QGraphicsItem* item, m_designerWidget->activePage()->selectedItems()){
2016-02-17 10:39:17 +03:00
BaseDesignIntf* bg = dynamic_cast<BaseDesignIntf*>(item);
if (bg){
ObjectBrowserNode* node = m_itemsMap.value(bg);
if (node)
node->setSelected(true);
}
2016-02-17 10:11:00 +03:00
}
2016-02-17 10:39:17 +03:00
m_changingItemSelection = false;
}
2016-02-17 10:11:00 +03:00
}
2016-02-17 10:18:19 +03:00
void ObjectBrowser::slotItemDoubleClicked(QTreeWidgetItem *item, int)
{
ObjectBrowserNode* node = dynamic_cast<ObjectBrowserNode*>(item);
if (node){
BaseDesignIntf* baseItem = dynamic_cast<BaseDesignIntf*>(node->object());
if (baseItem) {
baseItem->showEditorDialog();
}
}
}
void ObjectBrowser::slotActivePageUpdated(LimeReport::PageDesignIntf *)
{
buildTree();
}
2018-04-24 15:51:06 +03:00
void ObjectBrowser::moveItemNode(BaseDesignIntf* item, BaseDesignIntf* parent)
{
if (m_itemsMap.contains(item) && m_itemsMap.contains(parent)){
m_itemsMap.value(item)->parent()->removeChild(m_itemsMap.value(item));
m_itemsMap.value(parent)->addChild(m_itemsMap.value(item));
m_changingItemSelection = true;
m_itemsMap.value(item)->setSelected(true);
item->setSelected(true);
m_changingItemSelection = false;
}
2018-04-24 15:51:06 +03:00
}
2018-04-24 15:51:06 +03:00
void ObjectBrowser::slotItemParentChanged(BaseDesignIntf* item, BaseDesignIntf* parent)
{
if (!m_movingItem){
m_movingItem = true;
moveItemNode(item, parent);
m_movingItem = false;
foreach(QObject* di, m_defferedItems){
BaseDesignIntf* b = dynamic_cast<BaseDesignIntf*>(di);
if (b)
moveItemNode(b, parent);
}
m_defferedItems.clear();
} else {
if (!m_defferedItems.contains(item))
m_defferedItems.append(item);
}
}
2016-02-17 10:11:00 +03:00
void ObjectBrowserNode::setObject(QObject *value)
{
m_object = value;
}
QObject *ObjectBrowserNode::object() const
{
return m_object;
}
ObjectBrowserNode::ObjectBrowserNode(QTreeWidget *view)
:QTreeWidgetItem(view), m_object(0){}
ObjectBrowserNode::ObjectBrowserNode(QTreeWidgetItem *parent)
:QTreeWidgetItem(parent), m_object(0){}
bool ObjectBrowserNode::operator <(const QTreeWidgetItem &other) const
{
BandDesignIntf* band1 = dynamic_cast<BandDesignIntf*>(m_object);
BandDesignIntf* band2 = dynamic_cast<BandDesignIntf*>(dynamic_cast<const ObjectBrowserNode&>(other).object());
if (band1 && band2) return band1->bandIndex()<band2->bandIndex();
return false;
}
} //namespace LimeReport