#include "texteditor.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//Added by qt3to4:
#include
#include
#include
#include
#include
#include
#include "noteobj.h"
#include "version.h"
extern Q3Canvas* actCanvas;
extern int statusbarTime;
extern QSettings settings;
extern QAction *actionViewToggleNoteEditor;
extern QString iconPath;
using namespace std;
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
TextEditor::TextEditor()
{
printer = new QPrinter( QPrinter::HighResolution );
printer->setPrinterName (settings.readEntry("/vym/mainwindow/printerName",printer->printerName()));
// Editor (use MyTextEdit to wrap verticalAlignmentChanged to
// int argument. see header file)
e = new MyTextEdit( this, "editor" );
e->setFocus();
e->setTextFormat(Qt::RichText); // default
e->setTabStopWidth (20); // unit is pixel
e->setColor (Qt::black);
connect (e, SIGNAL( textChanged() ), this, SLOT( editorChanged() ) );
setCentralWidget( e );
statusBar()->message( "Ready", statusbarTime);
setCaption(__VYM " - " +tr ("Note Editor"));
// Toolbars
setupFileActions();
setupEditActions();
setupFormatActions();
setupSettingsActions();
// Various states
emptyPaper = QBrush(Qt::gray);
filledPaper = QBrush(Qt::white);
inactivePaper= QBrush(Qt::black);
setInactive();
// Load Settings
resize (settings.readNumEntry( "/vym/noteeditor/geometry/width", 450),
settings.readNumEntry( "/vym/noteeditor/geometry/height",600));
move (settings.readNumEntry( "/vym/noteeditor/geometry/posX", 150),
settings.readNumEntry( "/vym/noteeditor/geometry/posY", 50));
if (settings.readEntry( "/vym/noteeditor/showWithMain","yes") =="yes")
setShowWithMain(true);
else
setShowWithMain(false);
varFont.fromString( settings.readEntry
("/vym/noteeditor/fonts/varFont",
"Nimbus Sans l,14,-1,5,48,0,0,0,0,0")
);
fixedFont.fromString (settings.readEntry (
"/vym/noteeditor/fonts/fixedFont",
"Courier,14,-1,5,48,0,0,0,1,0")
);
QString s=settings.readEntry ("/vym/noteeditor/fonts/fonthintDefault","variable");
if (s == "fixed")
{
actionSettingsFonthintDefault->setOn (true);
e->setCurrentFont (fixedFont);
} else
{
actionSettingsFonthintDefault->setOn (false);
e->setCurrentFont (varFont);
}
filenameHint="";
// Save settings in vymrc
settings.writeEntry("/vym/mainwindow/printerName",printer->printerName());
}
TextEditor::~TextEditor()
{
if (printer) delete printer;
// Save Settings
settings.writeEntry( "/vym/noteeditor/geometry/width", width() );
settings.writeEntry( "/vym/noteeditor/geometry/height", height() );
settings.writeEntry( "/vym/noteeditor/geometry/posX", pos().x() );
settings.writeEntry( "/vym/noteeditor/geometry/posY", pos().y() );
if (showWithMain())
settings.writeEntry( "/vym/noteeditor/showWithMain","yes");
else
settings.writeEntry( "/vym/noteeditor/showWithMain","no");
QString s;
if (actionSettingsFonthintDefault->isOn() )
s="fixed";
else
s="variable";
settings.writeEntry( "/vym/noteeditor/fonts/fonthintDefault",s );
settings.writeEntry ("/vym/noteeditor/fonts/varFont",
varFont.toString() );
settings.writeEntry ("/vym/noteeditor/fonts/fixedFont",
fixedFont.toString() );
}
bool TextEditor::isEmpty()
{
if (e->text().length())
return false;
else
return true;
}
void TextEditor::setShowWithMain(bool v)
{
showwithmain=v;
}
bool TextEditor::showWithMain()
{
return showwithmain;
}
void TextEditor::setFontHint (const QString &fh)
{
if (fh=="fixed")
actionFormatUseFixedFont->setOn (true);
else
actionFormatUseFixedFont->setOn (false);
}
QString TextEditor::getFontHint()
{
if (actionFormatUseFixedFont->isOn())
return "fixed";
else
return "var";
}
QString TextEditor::getFontHintDefault()
{
if (actionSettingsFonthintDefault->isOn())
return "fixed";
else
return "var";
}
void TextEditor::setFilename(const QString &fn)
{
if (state==filledEditor)
if (fn.isEmpty() )
{
filename="";
statusBar()->message( "No filename available for this note.", statusbarTime );
}
else
{
filename=fn;
statusBar()->message( QString( "Current filename is %1" ).arg( filename ), statusbarTime );
}
}
QString TextEditor::getFilename()
{
return filename;
}
void TextEditor::setFilenameHint(const QString &fnh)
{
filenameHint=fnh;
}
QString TextEditor::getFilenameHint()
{
return filenameHint;
}
bool TextEditor::findText(const QString &t, const bool &cs)
{
bool wo=false; // word matches
if (e->find (t, cs, wo, true, 0, 0 ))
return true;
else
return false;
}
void TextEditor::setupFileActions()
{
QToolBar *tb = addToolBar ( tr("File Actions") );
QMenu *fileMenu = menuBar()->addMenu( tr( "&File" ));
QAction *a;
a = new QAction( QPixmap( iconPath+"fileopen.png"), tr( "&Import..." ),this);
a->setStatusTip (tr( "Import" ) );
a->setShortcut( Qt::CTRL + Qt::Key_O );
connect( a, SIGNAL( activated() ), this, SLOT( textLoad() ) );
tb->addAction (a);
fileMenu->addAction (a);
actionFileLoad=a;
fileMenu->addSeparator();
a = new QAction( QPixmap(iconPath+"filesave.png" ), tr( "&Export..." ),this);
a->setStatusTip (tr( "Export Note (HTML)" ) );
a->setShortcut( Qt::CTRL + Qt::Key_S );
connect( a, SIGNAL( activated() ), this, SLOT( textSave() ) );
tb->addAction (a);
fileMenu->addAction (a);
actionFileSave=a;
a = new QAction( QPixmap(), tr( "Export &As... (HTML)" ), this);
a->setStatusTip (tr( "Export Note As (HTML) " ));
connect( a, SIGNAL( activated() ), this, SLOT( textSaveAs() ) );
fileMenu->addAction (a);
actionFileSaveAs=a;
a = new QAction(QPixmap(), tr( "Export &As...(ASCII)" ), this);
a->setStatusTip ( tr( "Export Note As (ASCII) " ) );
a->setShortcut(Qt::ALT + Qt::Key_X );
connect( a, SIGNAL( activated() ), this, SLOT( textExportAsASCII() ) );
fileMenu->addAction (a);
actionFileSaveAs=a;
fileMenu->addSeparator();
a = new QAction( QPixmap(iconPath+"fileprint.png" ), tr( "&Print..." ),this);
a->setStatusTip (tr( "Print Note" ) );
a->setShortcut( Qt::CTRL + Qt::Key_P );
connect( a, SIGNAL( activated() ), this, SLOT( textPrint() ) );
tb->addAction (a);
fileMenu->addAction (a);
actionFilePrint=a;
}
void TextEditor::setupEditActions()
{
QToolBar *tb = addToolBar ( tr( "Edit Actions" ));
QMenu *editMenu = menuBar()->addMenu ( tr( "&Edit" ));
QAction *a;
a = new QAction(QPixmap(iconPath+"undo.png"), tr( "&Undo" ), this );
a->setStatusTip ( tr( "Undo" ) );
a->setShortcut(Qt::CTRL + Qt::Key_Z );
connect( a, SIGNAL( activated() ), e, SLOT( undo() ) );
editMenu->addAction (a);
tb->addAction (a);
actionEditUndo=a;
a = new QAction(QPixmap(iconPath+"redo.png" ), tr( "&Redo" ),this);
a->setStatusTip ( tr( "Redo" ) );
a->setShortcut( Qt::CTRL + Qt::Key_Y );
connect( a, SIGNAL( activated() ), e, SLOT( redo() ) );
editMenu->addAction (a);
tb->addAction (a);
actionEditRedo=a;
editMenu->addSeparator();
a = new QAction(QPixmap(), tr( "Select and copy &all" ),this);
a->setStatusTip ( tr( "Select and copy all" ) );
a->setShortcut( Qt::CTRL + Qt::Key_A );
connect( a, SIGNAL( activated() ), this, SLOT( editCopyAll() ) );
editMenu->addAction (a);
editMenu->addSeparator();
a = new QAction(QPixmap(iconPath+"editcopy.png" ), tr( "&Copy" ),this);
a->setStatusTip ( tr( "Copy" ) );
a->setShortcut( Qt::CTRL + Qt::Key_C );
connect( a, SIGNAL( activated() ), e, SLOT( copy() ) );
editMenu->addAction (a);
tb->addAction (a);
actionEditCopy=a;
a = new QAction(QPixmap(iconPath+"editcut.png" ), tr( "Cu&t" ),this);
a->setStatusTip ( tr( "Cut" ) );
a->setShortcut( Qt::CTRL + Qt::Key_X );
connect( a, SIGNAL( activated() ), e, SLOT( cut() ) );
editMenu->addAction (a);
tb->addAction (a);
actionEditCut=a;
a = new QAction(QPixmap(iconPath+"editpaste.png" ), tr( "&Paste" ),this);
a->setStatusTip ( tr( "Paste" ) );
a->setShortcut( Qt::CTRL + Qt::Key_V );
connect( a, SIGNAL( activated() ), e, SLOT( paste() ) );
editMenu->addAction (a);
tb->addAction (a);
actionEditPaste=a;
a = new QAction( QPixmap( iconPath+"edittrash.png"), tr( "&Delete All" ), this);
a->setStatusTip (tr( "Delete all" ) );
connect( a, SIGNAL( activated() ), e, SLOT( clear() ) );
editMenu->addAction (a);
tb->addAction (a);
actionEditDeleteAll=a;
a = new QAction(QPixmap(), tr( "&Convert Paragraphs" ),this);
a->setStatusTip(tr( "Convert paragraphs to linebreaks" ));
a->setShortcut( Qt::ALT + Qt::Key_P );
connect( a, SIGNAL( activated() ), this, SLOT( textConvertPar() ) );
editMenu->addAction (a);
actionEditConvertPar=a;
a = new QAction( QPixmap(), tr( "&Join lines" ), this);
a->setStatusTip(tr( "Join all lines of a paragraph" ) );
a->setShortcut(Qt::ALT + Qt::Key_J );
connect( a, SIGNAL( activated() ), this, SLOT( textJoinLines() ) );
editMenu->addAction (a);
actionEditJoinLines=a;
}
void TextEditor::setupFormatActions()
{
QToolBar *tb = addToolBar ( tr("Format Actions" ));
QMenu *formatMenu = menuBar()->addMenu ( tr( "F&ormat" ));
QAction *a;
a = new QAction( QPixmap(iconPath+"formatfixedfont.png"), tr( "&Font hint" ), Qt::ALT + Qt::Key_I, this, "fontHint" );
a->setStatusTip (tr( "Toggle font hint for the whole text" ) );
a->setToggleAction (true);
a->setOn (settings.readBoolEntry ("/vym/noteeditor/fonts/useFixedByDefault",false) );
connect( a, SIGNAL( activated() ), this, SLOT( toggleFonthint() ) );
formatMenu->addAction (a);
tb->addAction (a);
actionFormatUseFixedFont=a;
// comboFont = new QComboBox( true, tb );
comboFont = new QComboBox;
tb->addWidget (comboFont);
QFontDatabase db;
comboFont->insertStringList( db.families() );
connect( comboFont, SIGNAL( activated( const QString & ) ),
this, SLOT( textFamily( const QString & ) ) );
comboFont->addItem( QApplication::font().family() );
// comboSize = new QComboBox( true, tb );
comboSize = new QComboBox;
tb->addWidget (comboSize);
Q3ValueList sizes = db.standardSizes();
Q3ValueList::Iterator it = sizes.begin();
for ( ; it != sizes.end(); ++it )
comboSize->insertItem( QString::number( *it ) );
connect( comboSize, SIGNAL( activated( const QString & ) ),
this, SLOT( textSize( const QString & ) ) );
comboSize->addItem ( QString::number( QApplication::font().pointSize() ) );
formatMenu->addSeparator();
QPixmap pix( 16, 16 );
pix.fill( e->color());
a = new QAction( pix, tr( "&Color..." ), this);
formatMenu->addAction (a);
tb->addAction (a);
connect( a, SIGNAL( activated() ), this, SLOT( textColor() ) );
actionTextColor=a;
a = new QAction( QPixmap (iconPath+"text_bold.png"), tr( "&Bold" ), this);
a->setShortcut(Qt::CTRL + Qt::Key_B );
connect( a, SIGNAL( activated() ), this, SLOT( textBold() ) );
tb->addAction (a);
formatMenu->addAction (a);
a->setToggleAction( true );
actionTextBold=a;
a = new QAction( QPixmap(iconPath+"text_italic.png"), tr( "&Italic" ), this);
a->setShortcut(Qt::CTRL + Qt::Key_I);
connect( a, SIGNAL( activated() ), this, SLOT( textItalic() ) );
tb->addAction (a);
formatMenu->addAction (a);
a->setToggleAction( true );
actionTextItalic=a;
a = new QAction( QPixmap (iconPath+"text_under.png"), tr( "&Underline" ), this);
a->setShortcut(Qt::CTRL + Qt::Key_U );
connect( a, SIGNAL( activated() ), this, SLOT( textUnderline() ) );
tb->addAction (a);
formatMenu->addAction (a);
a->setToggleAction( true );
actionTextUnderline=a;
formatMenu->addSeparator();
QActionGroup *grp = new QActionGroup( this );
connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) );
a = new QAction( QPixmap (iconPath+"text_left.png"), tr( "&Left" ),grp );
a->setShortcut( Qt::CTRL+Qt::Key_L );
a->setToggleAction( true );
tb->addAction (a);
formatMenu->addAction (a);
actionAlignLeft=a;
a = new QAction( QPixmap (iconPath+"text_center.png"), tr( "C&enter" ),grp);
a->setShortcut( Qt::CTRL + Qt::Key_E);
a->setToggleAction( true );
tb->addAction (a);
formatMenu->addAction (a);
actionAlignCenter=a;
a = new QAction( QPixmap (iconPath+"text_right.png" ), tr( "&Right" ), grp);
a->setShortcut(Qt::CTRL + Qt::Key_R );
a->setToggleAction( true );
tb->addAction (a);
formatMenu->addAction (a);
actionAlignRight=a;
a = new QAction( QPixmap ( iconPath+"text_block.png"), tr( "&Justify" ), grp );
a->setShortcut(Qt::CTRL + Qt::Key_J );
a->setToggleAction( true );
tb->addAction (a);
formatMenu->addAction (a);
actionAlignJustify=a;
QActionGroup *grp2 = new QActionGroup( this );
grp2->setExclusive(true);
a = new QAction( QPixmap (iconPath+"text_sub.png"), tr( "Subs&cript" ),grp2 );
a->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_B );
a->setToggleAction( true );
tb->addAction (a);
formatMenu->addAction (a);
connect(a, SIGNAL(activated()), this, SLOT(textVAlign()));
actionAlignSubScript=a;
a = new QAction( QPixmap (iconPath+"text_super.png"), tr( "Su&perscript" ),grp2 );
a->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_P );
a->setToggleAction( true );
tb->addAction (a);
formatMenu->addAction (a);
connect(a, SIGNAL(activated()), this, SLOT(textVAlign()));
actionAlignSuperScript=a;
formatMenu->addSeparator();
connect( e, SIGNAL( currentFontChanged( const QFont & ) ),
this, SLOT( fontChanged( const QFont & ) ) );
connect( e, SIGNAL( currentColorChanged( const QColor & ) ),
this, SLOT( colorChanged( const QColor & ) ) );
connect( e, SIGNAL( currentAlignmentChanged( int ) ),
this, SLOT( alignmentChanged( int ) ) );
connect( e, SIGNAL( currentVerticalAlignmentChanged(int)),
this, SLOT( verticalAlignmentChanged(int)));
}
void TextEditor::setupSettingsActions()
{
QMenu *settingsMenu = menuBar()->addMenu ( tr( "&Settings" ));
QAction *a;
a = new QAction(tr( "Set &fixed font" ), this);
a->setStatusTip ( tr( "Set fixed font" ));
connect( a, SIGNAL( activated() ), this, SLOT( setFixedFont() ) );
settingsMenu->addAction (a);
actionSettingsFixedFont=a;
a = new QAction(tr( "Set &variable font" ), this);
a->setStatusTip ( tr( "Set variable font" ) );
connect( a, SIGNAL( activated() ), this, SLOT( setVarFont() ) );
settingsMenu->addAction (a);
actionSettingsVarFont=a;
a = new QAction(tr( "&fixed font is default" ), this);
a->setStatusTip (tr( "Used fixed font by default" ) );
a->setToggleAction (true);
// set state later in constructor...
settingsMenu->addAction (a);
actionSettingsFonthintDefault=a;
}
void TextEditor::textLoad()
{
if (state!=inactiveEditor)
{
if (e->length())
{
QMessageBox mb( __VYM " - " +tr("Note Editor"),
"Loading will overwrite the existing note",
QMessageBox::Warning,
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::Cancel,
0 );
mb.setButtonText( QMessageBox::Yes, "Load note" );
switch( mb.exec() ) {
case QMessageBox::Cancel:
return;
break;
}
}
// Load note
Q3FileDialog *fd=new Q3FileDialog( this);
fd->addFilter ("ASCII texts (*.txt)");
fd->addFilter ("VYM notes (*.html)");
fd->show();
QString fn;
if ( fd->exec() == QDialog::Accepted )
fn = fd->selectedFile();
if ( !fn.isEmpty() )
{
QFile f( fn );
if ( !f.open( QIODevice::ReadOnly ) )
return;
QTextStream ts( &f );
setText( ts.read() );
editorChanged();
}
}
}
void TextEditor::closeEvent( QCloseEvent* ce )
{
ce->accept(); // TextEditor can be reopened with show()
actionViewToggleNoteEditor->setOn (false);
showwithmain=false;
return;
}
QString TextEditor::getText()
{
return e->text();
}
void TextEditor::editorChanged()
{
if (isEmpty())
state=emptyEditor;
else
state=filledEditor;
if (state==emptyEditor)
e->setPaper (emptyPaper);
else
e->setPaper (filledPaper);
// SLOT is LinkableMapObj, which will update systemFlag
emit (textHasChanged() );
}
void TextEditor::setText(QString t)
{
if ( !Q3StyleSheet::mightBeRichText( t ) )
t = Q3StyleSheet::convertFromPlainText( t, Q3StyleSheetItem::WhiteSpaceNormal );
e->setReadOnly(false);
e->setText(t);
// editorChanged(); //not called automagically FIXME or is it?
enableActions();
}
void TextEditor::setInactive()
{
state=inactiveEditor;
setText("");
e->setPaper (inactivePaper);
e->setReadOnly (true);
disableActions();
}
void TextEditor::editCopyAll()
{
e->selectAll();
e->copy();
}
void TextEditor::textSaveAs()
{
QString fn = Q3FileDialog::getSaveFileName( QString::null, "VYM Note (HTML) (*.html);;All files (*)",
this,"export note dialog",tr("Export Note to single file") );
if ( !fn.isEmpty() )
{
QFile file (fn);
if (file.exists())
{
QMessageBox mb( __VYM,
tr("The file ") + fn +
tr(" exists already. "
"Do you want to overwrite it?"),
QMessageBox::Warning,
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::Cancel | QMessageBox::Escape,
Qt::NoButton );
mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
mb.setButtonText( QMessageBox::No, tr("Cancel"));
switch( mb.exec() ) {
case QMessageBox::Yes:
// save
filename = fn;
textSave();
return;
case QMessageBox::Cancel:
// do nothing
break;
}
} else
{
filename = fn;
textSave();
return;
}
}
statusBar()->message(tr( "Couldn't export note ") + fn, statusbarTime );
}
void TextEditor::textSave()
{
if ( filename.isEmpty() )
{
textSaveAs();
return;
}
QString text = e->text();
QFile f( filename );
if ( !f.open( QIODevice::WriteOnly ) )
{
statusBar()->message( QString("Could not write to %1").arg(filename),
statusbarTime );
return;
}
QTextStream t( &f );
t << text;
f.close();
e->setModified( FALSE );
statusBar()->message( QString( "Note exported as %1" ).arg( filename ), statusbarTime );
}
void TextEditor::textConvertPar()
{
// In X11 a copy&paste generates paragraphs,
// which is not always wanted
// This function replaces paragraphs by linebreaks.
int parFrom, parTo, indFrom, indTo;
e->getSelection (&parFrom,&indFrom,&parTo,&indTo);
QString t;
if (parFrom>-1)
t=e->selectedText();
else
t=e->text();
QRegExp re("");
re.setMinimal(true);
t.replace (re,"");
t.replace ("
","
");
if (parFrom>-1)
{
e->setCursorPosition (parFrom,indFrom);
e->cut();
// Tried to simply insert the changed text with
// e->insert (t,(uint)(QTextEdit::RemoveSelected));
// but then the html would be quoted. So I use the ugly
// way: insert a marker, replace it in whole text of QTextEdit
QString marker="R3PlAcEMeL4teR!";
e->insert (marker);
e->setText (e->text().replace(marker,t));
} else
e->setText(t);
}
void TextEditor::textJoinLines()
{
int parFrom, parTo, indFrom, indTo;
e->getSelection (&parFrom,&indFrom,&parTo,&indTo);
QString t;
if (parFrom>-1)
t=e->selectedText();
else
t=e->text();
// In addition to textConvertPar it is sometimes
// useful to join all lines of a paragraph
QRegExp re("\n+(?!
)");
re.setMinimal(true);
t.replace (re," ");
// Above we may have introduced new " " at beginning of a
// paragraph - remove it.
re.setPattern(" ");
t.replace (re,"
");
if (parFrom>-1)
{
e->setCursorPosition (parFrom,indFrom);
e->cut();
// Tried to simply insert the changed text with
// e->insert (t,(uint)(QTextEdit::RemoveSelected));
// but then the html would be quoted. So I use the ugly
// way: insert a marker, replace it in whole text of QTextEdit
QString marker="R3PlAcEMeL4teR!";
e->insert (marker);
e->setText (e->text().replace(marker,t));
} else
e->setText(t);
}
void TextEditor::textExportAsASCII()
{
QString text = NoteObj (e->text()).getNoteASCII();
QString fn,s;
if (!filenameHint.isEmpty())
{
if (!filenameHint.contains (".txt"))
s=filenameHint+".txt";
else
s=filenameHint;
} else
s=QString::null;
fn = Q3FileDialog::getSaveFileName( s, "VYM Note (ASCII) (*.txt);;All files (*)", this,"export note dialog",tr("Export Note to single file (ASCII)") );
int ret=-1;
if ( !fn.isEmpty() )
{
QFile file (fn);
if (file.exists())
{
QMessageBox mb( "VYM",
tr("The file ") + fn +
tr(" exists already. "
"Do you want to overwrite it?"),
QMessageBox::Warning,
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::Cancel | QMessageBox::Escape,
Qt::NoButton );
mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
mb.setButtonText( QMessageBox::No, tr("Cancel"));
ret=mb.exec();
}
if (ret==QMessageBox::Cancel)
return;
// save
if ( !file.open( QIODevice::WriteOnly ) )
statusBar()->message( QString("Could not write to %1").arg(filename),
statusbarTime );
else
{
QTextStream t( &file );
t << text;
file.close();
statusBar()->message( QString( "Note exported as %1" ).arg( fn ), statusbarTime );
}
}
}
void TextEditor::textPrint()
{
printer->setFullPage(TRUE);
if ( printer->setup( this ) )
{
QPainter p( printer );
// Check that there is a valid device to print to.
if ( !p.device() ) return;
Q3PaintDeviceMetrics metrics( p.device() );
int dpiy = metrics.logicalDpiY();
int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
QRect body( margin, margin, metrics.width() - 2*margin, metrics.height() - 2*margin );
QFont font( e->currentFont() );
font.setPointSize( 10 ); // we define 10pt to be a nice base size for printing
Q3SimpleRichText richText( e->text(), font,
e->context(),
e->styleSheet(),
e->mimeSourceFactory(),
body.height() );
richText.setWidth( &p, body.width() );
QRect view( body );
int page = 1;
do
{
richText.draw( &p, body.left(), body.top(), view, colorGroup() );
view.moveBy( 0, body.height() );
p.translate( 0 , -body.height() );
p.setFont( font );
p.drawText( view.right() - p.fontMetrics().width( QString::number( page ) ),
view.bottom() + p.fontMetrics().ascent() + 5, QString::number( page ) );
if ( view.top() >= richText.height() )
break;
printer->newPage();
page++;
} while (TRUE);
}
}
void TextEditor::textEditUndo()
{
}
void TextEditor::toggleFonthint()
{
setUpdatesEnabled (false);
e->selectAll (true);
if (!actionFormatUseFixedFont->isOn() )
e->setCurrentFont (varFont);
else
e->setCurrentFont (fixedFont);
e->selectAll (false);
setUpdatesEnabled (true);
repaint();
}
void TextEditor::setFixedFont()
{
bool ok;
QFont font =QFontDialog::getFont(
&ok, fixedFont, this );
if ( ok )
// font is set to the font the user selected
fixedFont=font;
}
void TextEditor::setVarFont()
{
bool ok;
QFont font =QFontDialog::getFont(
&ok, varFont, this );
if ( ok )
// font is set to the font the user selected
varFont=font;
}
void TextEditor::textBold()
{
e->setBold( actionTextBold->isOn() );
}
void TextEditor::textUnderline()
{
e->setUnderline( actionTextUnderline->isOn() );
}
void TextEditor::textItalic()
{
e->setItalic( actionTextItalic->isOn() );
}
void TextEditor::textFamily( const QString &f )
{
e->setFamily( f );
}
void TextEditor::textSize( const QString &p )
{
e->setPointSize( p.toInt() );
}
void TextEditor::textColor()
{
QColor col = QColorDialog::getColor( e->color(), this );
if ( !col.isValid() )
return;
e->setColor( col );
QPixmap pix( 16, 16 );
pix.fill( Qt::black );
actionTextColor->setIconSet( pix );
}
void TextEditor::textAlign( QAction *a )
{
if ( a == actionAlignLeft )
e->setAlignment( Qt::AlignLeft );
else if ( a == actionAlignCenter )
e->setAlignment( Qt::AlignHCenter );
else if ( a == actionAlignRight )
e->setAlignment( Qt::AlignRight );
else if ( a == actionAlignJustify )
e->setAlignment( Qt::AlignJustify );
}
void TextEditor::textVAlign()
{
if ( sender() == actionAlignSuperScript && actionAlignSuperScript->isOn()) {
e->setVerticalAlignment( Q3TextEdit::AlignSuperScript);
} else if (sender() == actionAlignSubScript && actionAlignSubScript->isOn()) {
e->setVerticalAlignment( Q3TextEdit::AlignSubScript);
} else {
e->setVerticalAlignment( Q3TextEdit::AlignNormal);
}
}
void TextEditor::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 TextEditor::colorChanged( const QColor &c )
{
QPixmap pix( 16, 16 );
pix.fill( c );
actionTextColor->setIconSet( pix );
}
void TextEditor::alignmentChanged( int a )
{
if ( ( a == Qt::AlignLeft ) || ( a & Qt::AlignLeft ))
actionAlignLeft->setOn( true );
else if ( ( a & Qt::AlignHCenter ) )
actionAlignCenter->setOn( true );
else if ( ( a & Qt::AlignRight ) )
actionAlignRight->setOn( true );
else if ( ( a & Qt::AlignJustify ) )
actionAlignJustify->setOn( true );
}
void TextEditor::verticalAlignmentChanged(int a)
{
if (a == Q3TextEdit::AlignSuperScript ) {
actionAlignSuperScript->setOn(true);
actionAlignSubScript->setOn(false);
} else if (a == Q3TextEdit::AlignSubScript ) {
actionAlignSuperScript->setOn(false);
actionAlignSubScript->setOn(true);
} else {
actionAlignSuperScript->setOn(false);
actionAlignSubScript->setOn(false);
}
}
void TextEditor::enableActions()
{
actionFileLoad->setEnabled(true);
actionFileSave->setEnabled(true);
actionFileSaveAs->setEnabled(true);
actionFilePrint->setEnabled(true);
actionEditUndo->setEnabled(true);
actionEditRedo->setEnabled(true);
actionEditCopy->setEnabled(true);
actionEditCut->setEnabled(true);
actionEditPaste->setEnabled(true);
actionEditDeleteAll->setEnabled(true);
actionEditConvertPar->setEnabled(true);
actionEditJoinLines->setEnabled(true);
actionFormatUseFixedFont->setEnabled(true);
}
void TextEditor::disableActions()
{
actionFileLoad->setEnabled(false);
actionFileSave->setEnabled(false);
actionFileSaveAs->setEnabled(false);
actionFilePrint->setEnabled(false);
actionEditUndo->setEnabled(false);
actionEditRedo->setEnabled(false);
actionEditCopy->setEnabled(false);
actionEditCut->setEnabled(false);
actionEditPaste->setEnabled(false);
actionEditDeleteAll->setEnabled(false);
actionEditConvertPar->setEnabled(false);
actionEditJoinLines->setEnabled(false);
actionFormatUseFixedFont->setEnabled(false);
}