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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2007 Martin Pfeiffer <hubipete@gmx.net>
0003                  2009 Jeremias Epperlein <jeeree@web.de>
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 "FormulaToolWidget.h"
0022 
0023 #include "KoFormulaTool.h"
0024 #include "KoFormulaShape.h"
0025 #include "ElementFactory.h"
0026 #include "BasicElement.h"
0027 #include "FormulaCursor.h"
0028 #include "FormulaDebug.h"
0029 
0030 #include <QWidgetAction>
0031 #include <QTableWidget>
0032 #include <QAction>
0033 #include <QHeaderView>
0034 #include <QMenu>
0035 
0036 FormulaToolWidget::FormulaToolWidget( KoFormulaTool* tool, QWidget* parent )
0037                   : QTabWidget( parent )
0038 {
0039     m_tool = tool;
0040     setupUi( this );
0041     // setup the element insert menus
0042     m_fractionMenu.addAction( m_tool->action( "insert_fraction" ) );
0043     m_fractionMenu.addAction( m_tool->action( "insert_bevelled_fraction" ) );
0044     
0045     
0046     m_fenceMenu.addAction( m_tool->action( "insert_fence" ) );
0047     m_fenceMenu.addAction( m_tool->action( "insert_enclosed" ) );
0048     
0049 
0050     m_tableMenu.addAction( m_tool->action( "insert_33table" ) );
0051     m_tableMenu.addAction( m_tool->action( "insert_21table" ) );
0052 
0053     m_rootMenu.addAction( m_tool->action( "insert_root" ) );
0054     m_rootMenu.addAction( m_tool->action( "insert_sqrt" ) );
0055 
0056     m_scriptsMenu.addAction( m_tool->action( "insert_subscript" ) );
0057     m_scriptsMenu.addAction( m_tool->action( "insert_supscript" ) );
0058     m_scriptsMenu.addAction( m_tool->action( "insert_subsupscript" ) );
0059     m_scriptsMenu.addAction( m_tool->action( "insert_underscript" ) );
0060     m_scriptsMenu.addAction( m_tool->action( "insert_overscript" ) );
0061     m_scriptsMenu.addAction( m_tool->action( "insert_underoverscript" ) );
0062     
0063 
0064     m_alterTableMenu.addAction( m_tool->action( "insert_row") );
0065     m_alterTableMenu.addAction( m_tool->action( "insert_column") );
0066     m_alterTableMenu.addAction( m_tool->action( "remove_row") );
0067     m_alterTableMenu.addAction( m_tool->action( "remove_column") );
0068 
0069     // assign menus to toolbuttons
0070     buttonFence->setMenu( &m_fenceMenu );
0071     buttonFence->setDefaultAction( m_tool->action( "insert_fence" ) );
0072     buttonRoot->setMenu( &m_rootMenu );
0073     buttonRoot->setDefaultAction( m_tool->action( "insert_sqrt" ) );
0074     buttonFraction->setMenu( &m_fractionMenu );
0075     buttonFraction->setDefaultAction(m_tool->action("insert_fraction"));
0076     buttonTable->setMenu( &m_tableMenu );
0077     buttonTable->setDefaultAction(m_tool->action( "insert_33table"));
0078     buttonScript->setMenu( &m_scriptsMenu );
0079     buttonScript->setDefaultAction(m_tool->action( "insert_subscript"));
0080 
0081     buttonAlterTable->setMenu(&m_alterTableMenu);
0082     buttonAlterTable->setDefaultAction(m_tool->action("insert_row"));
0083     // setup the buttons for symbol insertion
0084     buttonArrows->setText(QChar(0x2190));
0085     setupButton(buttonArrows,m_arrowMenu,i18n("Arrows"), symbolsInRange(0x2190,0x21FF));
0086     buttonGreek->setText(QChar(0x03B2));
0087     setupButton(buttonGreek,m_greekMenu,i18n("Greek"), symbolsInRange(0x0391,0x03A1)
0088                                                      <<symbolsInRange(0x03A3,0x03A9)
0089                                                      <<symbolsInRange(0x03B1,0x03C9));
0090     buttonRelation->setText(QChar(0x2265));
0091     setupButton(buttonRelation,m_relationMenu,i18n("Relations"), symbolsInRange(0x223C,0x2292)
0092                                                                <<symbolsInRange(0x2AAE,0x2ABA));
0093     buttonOperators->setText(QChar(0x2211));
0094     setupButton(buttonOperators,m_operatorMenu,i18n("Operators"), symbolsInRange(0x220F,0x2219)
0095                                                                <<symbolsInRange(0x2227,0x2233)
0096                                                                <<symbolsInRange(0x2207,0x2208));
0097     buttonMisc->setText(QChar(0x211A));
0098     setupButton(buttonMisc,m_miscMenu,i18n("Miscellaneous"), symbolsInRange(0x2200,0x2205)
0099                                                                    <<symbolsInRange(0x221F,0x2222));
0100 
0101     buttonRow->hide();
0102     connect( buttonLoad, SIGNAL(clicked()), m_tool, SLOT(loadFormula()) );
0103     connect( buttonSave, SIGNAL(clicked()), m_tool, SLOT(saveFormula()) );
0104     connect( buttonAlterTable, SIGNAL(triggered(QAction*)), m_tool, SLOT(changeTable(QAction*)));
0105 }
0106 
0107 FormulaToolWidget::~FormulaToolWidget()
0108 {}
0109 
0110 
0111 void FormulaToolWidget::setFormulaTool( KoFormulaTool* tool )
0112 {
0113     m_tool = tool;
0114 }
0115 
0116 
0117 void FormulaToolWidget::insertSymbol ( QTableWidgetItem* item )
0118 {
0119     m_tool->insertSymbol(item->text());
0120 }
0121 
0122 
0123 void FormulaToolWidget::setupButton ( QToolButton* button, QMenu& menu, const QString& text, QList<QString> list, int length)
0124 {
0125     QWidgetAction *widgetaction=new QWidgetAction(button);
0126     QTableWidget* table= new QTableWidget(list.length()/length,length,button);
0127     for (int i=0; i<list.length();i++) {
0128         QTableWidgetItem *newItem = new QTableWidgetItem(list[i]);
0129         newItem->setFlags(Qt::ItemIsEnabled);
0130         table->setItem(i/length,i%length, newItem);
0131     }
0132     table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0133     table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0134     table->horizontalHeader()->hide();
0135     table->verticalHeader()->hide();
0136     table->resizeColumnsToContents();
0137     table->resizeRowsToContents();
0138     table->setShowGrid(false);
0139     table->setFixedSize(table->horizontalHeader()->length(), table->verticalHeader()->length());
0140     button->setToolTip(text);
0141     //TODO: that is a little bit hackish
0142 //     connect( table,SIGNAL(itemActivated(QTableWidgetItem*)),
0143 //              table, SIGNAL(itemClicked(QTableWidgetItem*)));
0144     connect( table,SIGNAL(itemClicked(QTableWidgetItem*)),
0145              this, SLOT(insertSymbol(QTableWidgetItem*)));
0146     connect( table,SIGNAL(itemClicked(QTableWidgetItem*)),
0147              &menu, SLOT(hide()));
0148     button->setPopupMode(QToolButton::InstantPopup);
0149     button->setMenu(&menu);
0150     
0151     widgetaction->setDefaultWidget(table);
0152     menu.addAction(widgetaction);
0153 }
0154 
0155 QList< QString > FormulaToolWidget::symbolsInRange ( int first, int last )
0156 {
0157     QList<QString> list;
0158     for (int i=first;i<=last;++i) {
0159         list.append(QChar(i));
0160     }
0161     return list;
0162 }