/* * MathPlanner 3.0.7 - Mathematical design tool. * Copyright(C) 2002 Jarmo Nikkanen * * This program 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. * * You should have received a copy of the GNU General Public License with this program. * */ #include #include #include #include #include #include #include #include #include #include "Extern.h" #include "AppControl.h" #include "Error.h" #include "FrameObjects.h" #include "ConfigReader.h" PaperText::~PaperText() { if (edit) edit->close(true); if (rich) delete rich; } Editor *PaperText::Edit() { return(edit); } PaperText::PaperText(DataStorage *msg,ApplicationControl *a,Paper *p) :MathFrame(msg,a,p) { Enable(true,true,false); SetColor(QColor(0,0,255)); text=msg->ReadQString("text"); empty=false; edit=NULL; rich=NULL; CB=AppControl->TextControlBar; QFont font; rich=new QSimpleRichText(text,font); } PaperText::PaperText(QString b,ApplicationControl *a,Paper *p) :MathFrame(PAPER_OBJECT_TEXT,QRect(0,0,150,50),a,p) { Enable(true,true,false); SetColor(QColor(0,0,255)); empty=false; edit=NULL; rich=NULL; text=b; CB=AppControl->TextControlBar; if (b!=QString("_TextFrame")) { QFont font; rich=new QSimpleRichText(text,font); } else { text=""; Activate(); } } void PaperText::MessageReceived(DataStorage *msg,int rec) { if (rec==TO_FRAME) { if (IsActive()) { if (ISMSG(MSG_COPY)) editCopy(); if (ISMSG(MSG_PASTE)) editPaste(); if (ISMSG(MSG_CUT)) editCut(); } else MathFrame::MessageReceived(msg,rec); } } void PaperText::Activate() { if (IsActive()==false) { if (edit) ErrorReport("PaperText::Activate","Editor exists"); if (rich) { delete rich; rich=NULL; } edit = new Editor(paper,AppControl,this); CB=AppControl->TextControlBar; CB->CreateConnections(this); CB->Show(); edit->setVScrollBarMode(QScrollView::AlwaysOff); edit->setHScrollBarMode(QScrollView::AlwaysOff); edit->setFrameShape(QFrame::Box); //NoFrame); edit->setLineWidth(1); edit->setTextFormat( Qt::RichText ); edit->setText(text); edit->viewport()->setFocus(); } MathFrame::Activate(); // Must call the baseclass } void PaperText::DeActivate() { QFont font; if (IsActive()==true) { if (edit) { text=edit->text(); if (edit->close()) { delete edit; edit=NULL; } else ErrorReport("PaperText::DeActivate","Unable to close editor"); } if (rich) ErrorReport("PaperText::DeActivate","Rich exists"); rich=new QSimpleRichText(text,font); CB->Hide(); empty=text.isEmpty(); } MathFrame::DeActivate(); // Must call the baseclass } DataStorage *PaperText::BuildStorage() { if (edit) text=edit->text(); DataStorage *msg=MathFrame::BuildStorage(); msg->AddQString("text",text); return(msg); } const QString PaperText::Text() { if (edit) text=edit->text(); return(text); } void PaperText::SetBounds(QRect r) { MathFrame::SetBounds(r); view=bounds; view.setLeft(view.left()+10); view.setTop(view.top()+10); view.setRight(view.right()-10); view.setBottom(view.bottom()-10); } void PaperText::CalculateBounds() { MathFrame::CalculateBounds(); view=bounds; view.setLeft(view.left()+10); view.setTop(view.top()+10); view.setRight(view.right()-10); view.setBottom(view.bottom()-10); } void PaperText::MousePressed(QPoint p) { } void PaperText::Draw(QPainter *paint) { CalculateBounds(); if (IsActive() && edit) { edit->setGeometry(view); edit->show(); } if (rich && IsActive()==false) { rich->setWidth(paint,view.width()); const QColorGroup palette; const QRegion reg(view); int zoom=100; //AppControl->ZoomFactor(); QRect vi; vi.setLeft(view.left()*zoom/100); vi.setRight(view.right()*zoom/100); vi.setTop(view.top()*zoom/100); vi.setBottom(view.bottom()*zoom/100); paint->save(); paint->setClipRect(vi); rich->draw(paint,view.left(),view.top(),reg,palette); paint->restore(); } MathFrame::Draw(paint); } void PaperText::editCut() { if ( !edit ) return; edit->cut(); } void PaperText::editCopy() { if ( !edit ) return; edit->copy(); } void PaperText::editPaste() { if ( !edit ) return; edit->paste(); } void PaperText::textBold() { if ( !edit ) return; edit->setBold( CB->actionTextBold->isOn() ); } void PaperText::textUnderline() { if ( !edit ) return; edit->setUnderline( CB->actionTextUnderline->isOn() ); } void PaperText::textItalic() { if ( !edit ) return; edit->setItalic( CB->actionTextItalic->isOn() ); } void PaperText::textFamily( const QString &f ) { if ( !edit ) return; edit->setFamily( f ); edit->viewport()->setFocus(); } void PaperText::textSize( const QString &p ) { if ( !edit ) return; edit->setPointSize( p.toInt() ); edit->viewport()->setFocus(); } void PaperText::textStyle( int i ) { if ( !edit ) return; if ( i == 0 ) edit->setParagType( QStyleSheetItem::DisplayBlock, QStyleSheetItem::ListDisc ); else if ( i == 1 ) edit->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDisc ); else if ( i == 2 ) edit->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListCircle ); else if ( i == 3 ) edit->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListSquare ); else if ( i == 4 ) edit->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDecimal ); else if ( i == 5 ) edit->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListLowerAlpha ); else if ( i == 6 ) edit->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListUpperAlpha ); edit->viewport()->setFocus(); } void PaperText::textColor() { if ( !edit ) return; QColor col = QColorDialog::getColor( edit->color(), NULL ); if ( !col.isValid() ) return; edit->setColor( col ); CB->colorChanged( col ); } void PaperText::textAlign( QAction *a ) { if ( !edit ) return; if ( a == CB->actionAlignLeft ) edit->setAlignment( Qt::AlignLeft ); else if ( a == CB->actionAlignCenter ) edit->setAlignment( Qt::AlignHCenter ); else if ( a == CB->actionAlignRight ) edit->setAlignment( Qt::AlignRight ); else if ( a == CB->actionAlignJustify ) edit->setAlignment( Qt::AlignJustify ); } // ************************************** // Text Control ToolBar // ************************************** TextControl::~TextControl() { } TextControl::TextControl(ApplicationControl *a) :QObject() { AppControl=a; CreateToolBar(); } void TextControl::Show() { tb->setEnabled(true); tb->show(); } void TextControl::Hide() { if (AppControl->Prefs->Int("HideTextEdit")) tb->hide(); else tb->setEnabled(false); } void TextControl::CreateToolBar() { tb = new QToolBar( AppControl->MainWindow() ); tb->setLabel( "Format Actions" ); comboStyle = new QComboBox( FALSE, tb ); comboStyle->insertItem( "Standard" ); comboStyle->insertItem( "Bullet List (Disc)" ); comboStyle->insertItem( "Bullet List (Circle)" ); comboStyle->insertItem( "Bullet List (Square)" ); comboStyle->insertItem( "Ordered List (Decimal)" ); comboStyle->insertItem( "Ordered List (Alpha lower)" ); comboStyle->insertItem( "Ordered List (Alpha upper)" ); comboFont = new QComboBox( TRUE, tb ); QFontDatabase db; comboFont->insertStringList( db.families() ); comboFont->lineEdit()->setText( QApplication::font().family() ); comboSize = new QComboBox( TRUE, tb ); QValueList sizes = db.standardSizes(); QValueList::Iterator it = sizes.begin(); for ( ; it != sizes.end(); ++it ) comboSize->insertItem( QString::number( *it ) ); comboSize->lineEdit()->setText( QString::number( QApplication::font().pointSize() ) ); actionTextBold = new QAction( tr( "Bold" ), ICON("text_bold"), tr( "&Bold" ), CTRL + Key_B, AppControl->MainWindow(), "textBold" ); actionTextBold->addTo( tb ); actionTextBold->setToggleAction( TRUE ); actionTextItalic = new QAction( tr( "Italic" ), ICON("text_italic" ), tr( "&Italic" ), CTRL + Key_I, AppControl->MainWindow(), "textItalic" ); actionTextItalic->addTo( tb ); actionTextItalic->setToggleAction( TRUE ); actionTextUnderline = new QAction( tr( "Underline" ), ICON("text_under"), tr( "&Underline" ), CTRL + Key_U, AppControl->MainWindow(), "textUnderline" ); actionTextUnderline->addTo( tb ); actionTextUnderline->setToggleAction( TRUE ); grp = new QActionGroup( AppControl->MainWindow() ); grp->setExclusive( TRUE ); actionAlignLeft = new QAction( tr( "Left" ), ICON( "text_left" ), tr( "&Left" ), CTRL + Key_L, grp, "textLeft" ); actionAlignLeft->addTo( tb ); actionAlignLeft->setToggleAction( TRUE ); actionAlignCenter = new QAction( tr( "Center" ), ICON( "text_center" ), tr( "C&enter" ), CTRL + Key_E, grp, "textCenter" ); actionAlignCenter->addTo( tb ); actionAlignCenter->setToggleAction( TRUE ); actionAlignRight = new QAction( tr( "Right" ), ICON( "text_right" ), tr( "&Right" ), CTRL + Key_R, grp, "textRight" ); actionAlignRight->addTo( tb ); actionAlignRight->setToggleAction( TRUE ); actionAlignJustify = new QAction( tr( "Justify" ), ICON( "text_block" ), tr( "&Justify" ), CTRL + Key_J, grp, "textjustify" ); actionAlignJustify->addTo( tb ); actionAlignJustify->setToggleAction( TRUE ); QPixmap pix( 16, 16 ); pix.fill( black ); actionTextColor = new QAction( tr( "Color" ), pix, tr( "&Color..." ), 0, AppControl->MainWindow(), "textColor" ); actionTextColor->addTo( tb ); if (AppControl->Prefs->Int("HideTextEdit")) tb->hide(); else tb->show(); tb->setEnabled(false); } void TextControl::CreateConnections(class PaperText *pt) { // Connections to edit disconnect(comboStyle,0,0,0); disconnect(comboFont,0,0,0); disconnect(comboSize,0,0,0); disconnect(actionTextBold,0,0,0); disconnect(actionTextItalic,0,0,0); disconnect(actionTextUnderline,0,0,0); disconnect(grp,0,0,0); disconnect(actionTextColor,0,0,0); connect( comboStyle, SIGNAL( activated( int ) ), pt, SLOT( textStyle( int ) ) ); connect( comboFont,SIGNAL(activated(const QString &)),pt,SLOT(textFamily(const QString &))); connect( comboSize,SIGNAL(activated(const QString & )),pt, SLOT( textSize( const QString & ) ) ); connect( actionTextBold, SIGNAL( activated() ), pt, SLOT( textBold() ) ); connect( actionTextItalic, SIGNAL( activated() ), pt, SLOT( textItalic() ) ); connect( actionTextUnderline, SIGNAL( activated() ), pt, SLOT( textUnderline() ) ); connect( grp, SIGNAL( selected( QAction* ) ), pt, SLOT( textAlign( QAction* ) ) ); connect( actionTextColor, SIGNAL( activated() ), pt, SLOT( textColor() ) ); // Connections from edit if (pt->Edit()) { // Edit may return NULL connect( pt->Edit(), SIGNAL( currentFontChanged( const QFont & ) ),this, SLOT( fontChanged( const QFont & ) ) ); connect( pt->Edit(), SIGNAL( currentColorChanged( const QColor & ) ),this, SLOT( colorChanged( const QColor & ) ) ); connect( pt->Edit(), SIGNAL( currentAlignmentChanged( int ) ),this, SLOT( alignmentChanged( int ) ) ); // Init Setup fontChanged(pt->Edit()->font()); colorChanged(pt->Edit()->color()); alignmentChanged(pt->Edit()->alignment()); } else ErrorReport("TextControl::CreateConnections","No Editor"); } // SLOTS void TextControl::fontChanged( const QFont &f ) { comboFont->lineEdit()->setText( f.family() ); comboSize->lineEdit()->setText( QString::number( f.pointSize() ) ); actionTextBold->setOn( f.bold() ); actionTextItalic->setOn( f.italic() ); actionTextUnderline->setOn( f.underline() ); } void TextControl::colorChanged( const QColor &c ) { QPixmap pix( 16, 16 ); pix.fill( c ); actionTextColor->setIconSet( pix ); } void TextControl::alignmentChanged( int a ) { if ( ( a == AlignAuto ) || ( a & AlignLeft )) actionAlignLeft->setOn( TRUE ); else if ( ( a & AlignHCenter ) ) actionAlignCenter->setOn( TRUE ); else if ( ( a & AlignRight ) ) actionAlignRight->setOn( TRUE ); else if ( ( a & AlignJustify ) ) actionAlignJustify->setOn( TRUE ); } //*********** EDITOR CLASS ******* Editor::Editor(QWidget *parent,ApplicationControl *a,PaperText *f) :QTextEdit(parent) { frame=f; AppControl=a; } void Editor::focusInEvent( QFocusEvent *event ) { // Do NOT delete or DeActivate Editor in here frame->SetFocus(); // Be sure that SetFocus will not delete the Editor in any cases QTextEdit::focusInEvent(event); } void Editor::focusOutEvent( QFocusEvent *event ) { // Do NOT delete or DeActivate Editor in here QTextEdit::focusOutEvent(event); } void Editor::mousePressEvent(QMouseEvent *me) { // Do NOT delete or DeActivate Editor in here QTextEdit::mousePressEvent(me); }