kformula/flake

FormulaToolWidget.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2007 Martin Pfeiffer <hubipete@gmx.net>
00003                  2009 Jeremias Epperlein <jeeree@web.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "FormulaToolWidget.h"
00022 #include "KoFormulaTool.h"
00023 #include "KoFormulaShape.h"
00024 #include "ElementFactory.h"
00025 #include "BasicElement.h"
00026 #include <QWidgetAction>
00027 #include <QTableWidget>
00028 
00029 #include <KAction>
00030 
00031 #include <KMessageBox>
00032 #include <QMenu>
00033 #include <kdebug.h>
00034 #include "FormulaCursor.h"
00035 
00036 FormulaToolWidget::FormulaToolWidget( KoFormulaTool* tool, QWidget* parent )
00037                   : QTabWidget( parent )
00038 {
00039     m_tool = tool;
00040     setupUi( this );
00041     // setup the element insert menus
00042     m_fractionMenu.addAction( m_tool->action( "insert_fraction" ) );
00043     m_fractionMenu.addAction( m_tool->action( "insert_bevelled_fraction" ) );
00044     
00045     
00046     m_fenceMenu.addAction( m_tool->action( "insert_fence" ) );
00047     m_fenceMenu.addAction( m_tool->action( "insert_enclosed" ) );
00048     
00049 
00050     m_tableMenu.addAction( m_tool->action( "insert_33table" ) );
00051     m_tableMenu.addAction( m_tool->action( "insert_21table" ) );
00052 
00053     m_rootMenu.addAction( m_tool->action( "insert_root" ) );
00054     m_rootMenu.addAction( m_tool->action( "insert_sqrt" ) );
00055 
00056     m_scriptsMenu.addAction( m_tool->action( "insert_subscript" ) );
00057     m_scriptsMenu.addAction( m_tool->action( "insert_supscript" ) );
00058     m_scriptsMenu.addAction( m_tool->action( "insert_subsupscript" ) );
00059     m_scriptsMenu.addAction( m_tool->action( "insert_underscript" ) );
00060     m_scriptsMenu.addAction( m_tool->action( "insert_overscript" ) );
00061     m_scriptsMenu.addAction( m_tool->action( "insert_underoverscript" ) );
00062     
00063 
00064     m_alterTableMenu.addAction( m_tool->action( "insert_row") );
00065     m_alterTableMenu.addAction( m_tool->action( "insert_column") );
00066     m_alterTableMenu.addAction( m_tool->action( "remove_row") );
00067     m_alterTableMenu.addAction( m_tool->action( "remove_column") );
00068 
00069     // assign menus to toolbuttons
00070     buttonFence->setMenu( &m_fenceMenu );
00071     buttonFence->setDefaultAction( m_tool->action( "insert_fence" ) );
00072     buttonRoot->setMenu( &m_rootMenu );
00073     buttonRoot->setDefaultAction( m_tool->action( "insert_sqrt" ) );
00074     buttonFraction->setMenu( &m_fractionMenu );
00075     buttonFraction->setDefaultAction(m_tool->action("insert_fraction"));
00076     buttonTable->setMenu( &m_tableMenu );
00077     buttonTable->setDefaultAction(m_tool->action( "insert_33table"));
00078     buttonScript->setMenu( &m_scriptsMenu );
00079     buttonScript->setDefaultAction(m_tool->action( "insert_subscript"));
00080 
00081     buttonAlterTable->setMenu(&m_alterTableMenu);
00082     buttonAlterTable->setDefaultAction(m_tool->action("insert_row"));
00083     // setup the buttons for symbol insertion
00084     buttonArrows->setText(QChar(0x2190));
00085     setupButton(buttonArrows,m_arrowMenu,i18n("Arrows"), symbolsInRange(0x2190,0x21FF));
00086     buttonGreek->setText(QChar(0x03B2));
00087     setupButton(buttonGreek,m_greekMenu,i18n("Greek"), symbolsInRange(0x0391,0x03A1)
00088                                                      <<symbolsInRange(0x03A3,0x03A9)
00089                                                      <<symbolsInRange(0x03B1,0x03C9));
00090     buttonRelation->setText(QChar(0x2265));
00091     setupButton(buttonRelation,m_relationMenu,i18n("Relations"), symbolsInRange(0x223C,0x2292)
00092                                                                <<symbolsInRange(0x2AAE,0x2ABA));
00093     buttonOperators->setText(QChar(0x2211));
00094     setupButton(buttonOperators,m_operatorMenu,i18n("Operators"), symbolsInRange(0x220F,0x2219)
00095                                                                <<symbolsInRange(0x2227,0x2233)
00096                                                                <<symbolsInRange(0x2207,0x2208));
00097     buttonMisc->setText(QChar(0x211A));
00098     setupButton(buttonMisc,m_miscMenu,i18n("Miscellaneous"), symbolsInRange(0x2200,0x2205)
00099                                                                    <<symbolsInRange(0x221F,0x2222));
00100 
00101     buttonRow->hide();
00102     connect( buttonLoad, SIGNAL( clicked() ), m_tool, SLOT( loadFormula() ) );
00103     connect( buttonSave, SIGNAL( clicked() ), m_tool, SLOT( saveFormula() ) );
00104     connect( buttonAlterTable, SIGNAL( triggered( QAction* ) ), m_tool, SLOT( changeTable(QAction*)));
00105 }
00106 
00107 FormulaToolWidget::~FormulaToolWidget()
00108 {}
00109 
00110 
00111 void FormulaToolWidget::setFormulaTool( KoFormulaTool* tool )
00112 {
00113     m_tool = tool;
00114 }
00115 
00116 
00117 void FormulaToolWidget::insertSymbol ( QTableWidgetItem* item )
00118 {
00119     m_tool->insertSymbol(item->text());
00120 }
00121 
00122 
00123 void FormulaToolWidget::setupButton ( QToolButton* button, QMenu& menu, const QString& text, QList<QString> list, int length)
00124 {
00125     QWidgetAction *widgetaction=new QWidgetAction(button);
00126     QTableWidget* table= new QTableWidget(list.length()/length,length,button);
00127     for (int i=0; i<list.length();i++) {
00128         QTableWidgetItem *newItem = new QTableWidgetItem(list[i]);
00129         newItem->setFlags(Qt::ItemIsEnabled);
00130         table->setItem(i/length,i%length, newItem);
00131     }
00132     table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00133     table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00134     table->horizontalHeader()->hide();
00135     table->verticalHeader()->hide();
00136     table->resizeColumnsToContents();
00137     table->resizeRowsToContents();
00138     table->setShowGrid(false);
00139     table->setFixedSize(table->horizontalHeader()->length(), table->verticalHeader()->length());
00140     button->setToolTip(text);
00141     //TODO: that is a little bit hackish
00142 //     connect( table,SIGNAL( itemActivated(QTableWidgetItem*)),
00143 //              table, SIGNAL( itemClicked(QTableWidgetItem*)));
00144     connect( table,SIGNAL( itemClicked(QTableWidgetItem*)),
00145              this, SLOT( insertSymbol(QTableWidgetItem*)));
00146     connect( table,SIGNAL( itemClicked(QTableWidgetItem*)),
00147              &menu, SLOT(hide()));
00148     button->setPopupMode(QToolButton::InstantPopup);
00149     button->setMenu(&menu);
00150     
00151     widgetaction->setDefaultWidget(table);
00152     menu.addAction(widgetaction);
00153 }
00154 
00155 QList< QString > FormulaToolWidget::symbolsInRange ( int first, int last )
00156 {
00157     QList<QString> list;
00158     for (int i=first;i<=last;++i) {
00159         list.append(QChar(i));
00160     }
00161     return list;
00162 }
00163 
00164 
00165 #include "FormulaToolWidget.moc"