File indexing completed on 2024-05-12 16:34:59

0001 /* This file is part of the Calligra project, made within the KDE community.
0002  *
0003  * Copyright (C) 2013 Friedrich W. H. Kossebau <friedrich@kogmbh.com>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "TextDocumentInspectionDocker.h"
0022 
0023 #include "TextDocumentStructureModel.h"
0024 #include "TextShape.h"
0025 #include "KoTextShapeData.h"
0026 //
0027 #include <KoShapeManager.h>
0028 #include <KoCanvasBase.h>
0029 #include <KoSelection.h>
0030 // Qt
0031 #include <QDebug>
0032 #include <QTreeView>
0033 
0034 
0035 const int MsecsThresholdForMergingCommands = 2000;
0036 
0037 
0038 TextDocumentInspectionDocker::TextDocumentInspectionDocker(QWidget * parent)
0039     : QDockWidget(parent)
0040     , m_canvas(0)
0041     , m_mainWidget(new QTreeView(this))
0042     , m_textDocumentStructureModel(new TextDocumentStructureModel(this))
0043 {
0044     setWindowTitle(QLatin1String("TextDocument Inspector"));
0045     setWidget(m_mainWidget);
0046 
0047 //     m_mainWidget->setRootIsDecorated(false);
0048     m_mainWidget->setAllColumnsShowFocus(true);
0049     m_mainWidget->setUniformRowHeights(true);
0050     m_mainWidget->setAlternatingRowColors(true);
0051     m_mainWidget->setModel(m_textDocumentStructureModel);
0052 }
0053 
0054 TextDocumentInspectionDocker::~TextDocumentInspectionDocker()
0055 {
0056 }
0057 
0058 
0059 void TextDocumentInspectionDocker::setCanvas(KoCanvasBase *canvas)
0060 {
0061     setEnabled(canvas != 0);
0062 
0063     if (m_canvas) {
0064         m_canvas->disconnectCanvasObserver(this);
0065     }
0066 
0067     m_canvas = canvas;
0068     if (! m_canvas) {
0069         m_textDocumentStructureModel->setTextDocument(0);
0070         return;
0071     }
0072 
0073     connect(m_canvas->shapeManager(), SIGNAL(selectionChanged()),
0074             this, SLOT(onShapeSelectionChanged()));
0075 
0076     onShapeSelectionChanged();
0077 }
0078 
0079 void TextDocumentInspectionDocker::unsetCanvas()
0080 {
0081     setEnabled(false);
0082     m_textDocumentStructureModel->setTextDocument(0);
0083     m_canvas = 0;
0084 }
0085 
0086 void TextDocumentInspectionDocker::onShapeSelectionChanged()
0087 {
0088     QTextDocument* textDocument = 0;
0089 
0090     // TODO: big fail: shapeManager of a canvas still emits signals after unsetCanvas()
0091     // was called on us. And at least by the current API dox there is no way in unsetCanvas()
0092     // to get the shapeManager anymore, as "it's dead".
0093     // So we need to guard us here, this can be called when we officially are not
0094     // connected to any canvas.
0095     // Cmp. StrokeDocker::selectionChanged()
0096     if (m_canvas) {
0097         KoShape *shape = m_canvas->shapeManager()->selection()->firstSelectedShape();
0098         if (shape) {
0099             TextShape *textShape = dynamic_cast<TextShape*>(shape);
0100             if (textShape) {
0101                 textDocument = textShape->textShapeData()->document();
0102             }
0103         }
0104     }
0105 
0106     m_textDocumentStructureModel->setTextDocument(textDocument);
0107     m_mainWidget->expandToDepth(1);
0108 }