File indexing completed on 2024-05-12 16:36:02
0001 /* This file is part of the KDE project 0002 Copyright 2011 Marijn Kruisselbrink <mkruisselbrink@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "CellEditorDocker.h" 0021 0022 // Qt 0023 #include <QGridLayout> 0024 #include <QResizeEvent> 0025 #include <QToolButton> 0026 0027 // KF5 0028 #include <KLocalizedString> 0029 0030 // Calligra 0031 #include <KoToolProxy.h> 0032 #include <KoToolManager.h> 0033 0034 // Calligra Sheets 0035 #include "SheetsDebug.h" 0036 #include "CellToolBase.h" 0037 #include "ExternalEditor.h" 0038 #include "LocationComboBox.h" 0039 #include "SheetView.h" 0040 #include "part/CanvasBase.h" 0041 0042 using namespace Calligra::Sheets; 0043 0044 class CellEditorDocker::Private 0045 { 0046 public: 0047 CanvasBase *canvas; 0048 LocationComboBox *locationComboBox; 0049 QToolButton *formulaButton, *applyButton, *cancelButton; 0050 ExternalEditor *editor; 0051 QGridLayout *layout; 0052 CellToolBase *cellTool; 0053 QPointer<KoToolProxy> toolProxy; 0054 bool canvasResetBugWorkaround; 0055 }; 0056 0057 CellEditorDocker::CellEditorDocker() 0058 : d(new Private) 0059 { 0060 setWindowTitle(i18n("Cell Editor")); 0061 0062 d->canvas = 0; 0063 d->canvasResetBugWorkaround = false; 0064 0065 QWidget* w = new QWidget(this); 0066 0067 d->locationComboBox = new LocationComboBox(w); 0068 d->locationComboBox->setMinimumWidth(100); 0069 0070 d->formulaButton = new QToolButton(w); 0071 d->formulaButton->setText(i18n("Formula")); 0072 0073 d->editor = new ExternalEditor(w); 0074 d->editor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding); 0075 // d->editor->setMinimumHeight(d->locationComboBox->height()); 0076 0077 d->applyButton = new QToolButton(w); 0078 d->applyButton->setDefaultAction(d->editor->applyAction()); 0079 0080 d->cancelButton = new QToolButton(w); 0081 d->cancelButton->setDefaultAction(d->editor->cancelAction()); 0082 0083 d->layout = new QGridLayout(w); 0084 d->layout->setObjectName(QLatin1String("CellToolOptionWidget::Layout")); 0085 d->layout->addWidget(d->locationComboBox, 0, 0, Qt::AlignTop); 0086 d->layout->addWidget(d->formulaButton, 0, 1, Qt::AlignTop); 0087 d->layout->addWidget(d->applyButton, 0, 2, Qt::AlignTop); 0088 d->layout->addWidget(d->cancelButton, 0, 3, Qt::AlignTop); 0089 d->layout->addWidget(d->editor, 0, 4); 0090 d->layout->setColumnStretch(4, 1); 0091 0092 // w->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); 0093 setWidget(w); 0094 } 0095 0096 CellEditorDocker::~CellEditorDocker() 0097 { 0098 delete d; 0099 } 0100 0101 void CellEditorDocker::setCanvas(KoCanvasBase *canvas) 0102 { 0103 setEnabled(canvas != 0); 0104 0105 d->canvasResetBugWorkaround = !!d->canvas; 0106 if (d->toolProxy) { 0107 disconnect(d->toolProxy, SIGNAL(toolChanged(QString)), this, SLOT(toolChanged(QString))); 0108 } 0109 d->canvas = dynamic_cast<CanvasBase*>(canvas); 0110 if (d->canvas) { 0111 d->locationComboBox->setSelection(d->canvas->selection()); 0112 d->toolProxy = d->canvas->toolProxy(); 0113 connect(d->toolProxy, SIGNAL(toolChanged(QString)), this, SLOT(toolChanged(QString))); 0114 } 0115 } 0116 0117 void CellEditorDocker::unsetCanvas() 0118 { 0119 if (d->canvasResetBugWorkaround) return; 0120 debugSheets << "unsetting canvas"; 0121 if (d->toolProxy) { 0122 disconnect(d->toolProxy, SIGNAL(toolChanged(QString)), this, SLOT(toolChanged(QString))); 0123 } 0124 d->canvas = 0; 0125 d->toolProxy = 0; 0126 d->locationComboBox->setSelection(0); 0127 } 0128 0129 void CellEditorDocker::resizeEvent(QResizeEvent *event) 0130 { 0131 const int margin = 2 * d->layout->margin(); 0132 const int newWidth = event->size().width(); 0133 const int minWidth = d->layout->minimumSize().width(); 0134 // The triggering width is the same in both cases, but it is calculated in 0135 // different ways. 0136 // After a row got occupied, it does not vanish anymore, even if all items 0137 // get removed. Hence, check for the existence of the item in the 2nd row. 0138 if (!d->layout->itemAtPosition(1, 0)) { /* one row */ 0139 const int column = d->layout->count() - 1; 0140 QLayoutItem *const item = d->layout->itemAtPosition(0, column); 0141 if (!item) { 0142 QDockWidget::resizeEvent(event); 0143 return; 0144 } 0145 const int itemWidth = item->minimumSize().width(); 0146 if (newWidth <= 2 *(minWidth - itemWidth) + margin) { 0147 d->layout->removeItem(item); 0148 d->layout->addItem(item, 1, 0, 1, column + 1); 0149 d->layout->setRowStretch(0, 0); 0150 d->layout->setRowStretch(1, 1); 0151 } 0152 } else { /* two rows */ 0153 if (newWidth > 2 * minWidth + margin) { 0154 QLayoutItem *const item = d->layout->itemAtPosition(1, 0); 0155 d->layout->removeItem(item); 0156 d->layout->addItem(item, 0, d->layout->count()); 0157 d->layout->setRowStretch(0, 1); 0158 d->layout->setRowStretch(1, 0); 0159 } 0160 } 0161 QDockWidget::resizeEvent(event); 0162 } 0163 0164 void CellEditorDocker::toolChanged(const QString &toolId) 0165 { 0166 debugSheets << "tool changed to" << toolId; 0167 0168 const bool isCellTool = toolId == QLatin1String("KSpreadCellToolId"); 0169 setEnabled(isCellTool); 0170 0171 if (isCellTool) { 0172 KoToolBase* tool = KoToolManager::instance()->toolById(d->canvas, toolId); 0173 d->cellTool = qobject_cast<CellToolBase*>(tool); 0174 Q_ASSERT(d->cellTool); 0175 d->editor->setCellTool(d->cellTool); 0176 d->cellTool->setExternalEditor(d->editor); 0177 d->formulaButton->setDefaultAction(d->cellTool->action("insertFormula")); 0178 debugSheets << tool << d->cellTool; 0179 } 0180 } 0181 0182 CellEditorDockerFactory::CellEditorDockerFactory() 0183 { 0184 } 0185 0186 QString CellEditorDockerFactory::id() const 0187 { 0188 return QString::fromLatin1("CalligraSheetsCellEditor"); 0189 } 0190 0191 QDockWidget* CellEditorDockerFactory::createDockWidget() 0192 { 0193 CellEditorDocker* widget = new CellEditorDocker(); 0194 widget->setObjectName(id()); 0195 0196 return widget; 0197 }