📄 textedit.cpp
字号:
if ( fontsize[i] > sz ) { s = fontsize[i]; break; } } } QFont f = editor->font(); f.setPointSize(s); editor->setFont(f); // // Zooming only makes sense if we have more than one font size. // if (nfontsizes > 1) { zin->setEnabled(s != fontsize[nfontsizes-1]); zout->setEnabled(s != fontsize[0]); zinE = zin->isEnabled(); zoutE = zout->isEnabled(); }}void TextEdit::setBold(bool y){ QFont f = editor->font(); f.setBold(y); editor->setFont(f);}void TextEdit::setItalic(bool y){ QFont f = editor->font(); f.setItalic(y); editor->setFont(f);}void TextEdit::setWordWrap(bool y){ bool state = editor->edited(); editor->setWordWrap(y ? QMultiLineEdit::WidgetWidth : QMultiLineEdit::NoWrap ); editor->setEdited( state );}void TextEdit::setFixedWidth(bool y){ if (y) { editor->setFont(QFont("fixed")); zinE = zin->isEnabled(); zoutE = zout->isEnabled(); zin->setEnabled(FALSE); zout->setEnabled(FALSE); } else { editor->setFont(QFont()); setFontSize(variableFontSize,zoomOutLast); zin->setEnabled(zinE); zout->setEnabled(zoutE); }}void TextEdit::clipboardChanged(){ pasteAction->setEnabled( !qApp->clipboard()->text().isEmpty() );}void TextEdit::linkChanged( const QString &linkfile ){ if ( doc ) { DocLnk dl( linkfile ); if ( doc->linkFileKnown() && doc->linkFile() == linkfile || doc->fileKnown() && (doc->file() == linkfile || dl.isValid() && dl.file() == doc->file()) ) { if ( !QFile::exists(doc->file()) && !QFile::exists(doc->linkFile()) ) { // deleted fileRevert(); } else { if ( doc->name() != dl.name() ) updateCaption(dl.name()); *doc = dl; } } }}void TextEdit::fileNew(){ save(); newFile(DocLnk());}void TextEdit::fileOpen(){ if ( !save() ) { if ( QMessageBox::critical( this, tr( "Out of space" ), tr( "Text Editor was unable to\n" "save your changes.\n" "Free some space and try again.\n" "\nContinue anyway?" ), QMessageBox::Yes|QMessageBox::Escape, QMessageBox::No|QMessageBox::Default ) != QMessageBox::Yes ) return; else { delete doc; doc = 0; } } menu->hide(); editBar->hide(); if (searchBar) searchBar->hide(); editorStack->raiseWidget( fileSelector ); updateCaption();}void TextEdit::fileRevert(){ clear(); fileOpen();}void TextEdit::editCut(){#ifndef QT_NO_CLIPBOARD editor->cut();#endif}void TextEdit::editCopy(){#ifndef QT_NO_CLIPBOARD editor->copy();#endif}void TextEdit::editPaste(){#ifndef QT_NO_CLIPBOARD editor->paste();#endif}void TextEdit::editFind(bool s){ if ( !searchBar ) { searchBar = new QPEToolBar(this); addToolBar( searchBar, tr("Search"), QMainWindow::Top, TRUE ); searchBar->setHorizontalStretchable( TRUE ); searchEdit = new QLineEdit( searchBar, "searchEdit" ); searchBar->setStretchableWidget( searchEdit ); connect( searchEdit, SIGNAL( textChanged( const QString & ) ), this, SLOT( search() ) ); connect( searchEdit, SIGNAL(returnPressed()), this, SLOT(search()) ); QAction *a = new QAction( tr( "Find Next" ), Resource::loadIconSet( "next" ), QString::null, 0, this, 0 ); connect( a, SIGNAL(activated()), this, SLOT(search()) ); a->setWhatsThis( tr("Find the next occurrence of the search text.") ); a->addTo( searchBar ); } if ( s ) { searchBar->show(); searchVisible = TRUE; searchEdit->setFocus(); } else { searchVisible = FALSE; searchBar->hide(); }}void TextEdit::search(){ editor->find( searchEdit->text(), FALSE );}void TextEdit::findWrapped(){ Global::statusMessage( tr("Find: reached end") );}void TextEdit::findNotFound(){ Global::statusMessage( tr("Find: not found") );}void TextEdit::findFound(){ Global::statusMessage( "" );}void TextEdit::newFile( const DocLnk &f ){ DocLnk nf = f; nf.setType("text/plain"); clear(); editorStack->raiseWidget( editor ); editor->setFocus(); doc = new DocLnk(nf); setReadOnly(FALSE); updateCaption();}void TextEdit::setDocument(const QString& f){ DocLnk nf(f); nf.setType("text/plain"); openFile(nf); showEditTools(); // Show filename in caption QString name; if ( nf.linkFileKnown() && !nf.name().isEmpty() ) { name = nf.name(); } else { name = f; int sep = name.findRev( '/' ); if ( sep > 0 ) name = name.mid( sep+1 ); } updateCaption( name );}void TextEdit::openFile( const DocLnk &f ){ clear(); FileManager fm; QString txt; QByteArray ba; bool needsave = FALSE; if ( fm.loadFile( f, ba ) ) { txt = QString::fromUtf8(ba, ba.size()); if ( txt.utf8().length() != ba.size() ) { // not UTF8 QTextCodec* codec = QTextCodec::codecForContent(ba.data(),ba.size()); if ( codec ) { txt = codec->toUnicode(ba); needsave = TRUE; } } } fileNew(); if ( doc ) delete doc; doc = new DocLnk(f); editor->setText(txt); editor->setEdited(needsave); updateCaption();}void TextEdit::showEditTools(){ if ( !doc ) close(); fileSelector->hide(); menu->show(); editBar->show(); if ( searchBar && searchVisible ) searchBar->show(); updateCaption();}bool TextEdit::save(){ // case of nothing to save... if ( !doc ) return true; if ( !editor->edited() ) { delete doc; doc = 0; return true; } QString rt = editor->text(); if ( doc->name().isEmpty() ) doc->setName(calculateName(rt)); FileManager fm; if ( !fm.saveFile( *doc, rt ) ) { return false; } delete doc; doc = 0; editor->setEdited( false ); return true;}QString TextEdit::calculateName(QString rt) { QString pt = rt.simplifyWhiteSpace(); int i = pt.find( ' ' ); QString docname = pt; if ( i > 0 ) docname = pt.left( i ); // remove "." at the beginning while( docname.startsWith( "." ) ) docname = docname.mid( 1 ); docname.replace( QRegExp("/"), "_" ); // cut the length. filenames longer than that don't make sense and something goes wrong when they get too long. if ( docname.length() > 40 ) docname = docname.left(40); if ( docname.isEmpty() ) docname = tr("Empty Text"); return docname;}void TextEdit::fileName(){ if (!doc) newFile(DocLnk()); if (doc->name().isEmpty()) doc->setName(calculateName(editor->text())); // // Document properties operations depend on the file being // up-to-date. Force a write before changing properties. // FileManager fm; if ( !fm.saveFile( *doc, editor->text() ) ) { return; } DocPropertiesDialog *lp = new DocPropertiesDialog(doc, this); QPEApplication::execDialog( lp ); delete lp; updateCaption(doc->name());}void TextEdit::clear(){ delete doc; doc = 0; editor->clear();}void TextEdit::updateCaption( const QString &name ){ if ( !doc ) setCaption( tr("Text Editor") ); else { QString s = name; if ( s.isNull() ) s = doc->name(); if ( s.isEmpty() ) s = tr( "Unnamed" ); setCaption( s + " - " + tr("Text Editor") ); }}void TextEdit::accept(){ fileOpen();} void TextEdit::message(const QCString& msg, const QByteArray& data){ if ( msg == "viewFile(QString)" || msg == "openFile(QString)" ) { QDataStream d(data,IO_ReadOnly); QString filename; d >> filename; // // .desktop files should _not_ be able to be edited easily, // as they are generated by the server. Force opening the // file they refer to, rather than the .desktop file. // if (!filename.contains(".desktop")) { if (filename.stripWhiteSpace().isEmpty()){ newFile(DocLnk()); }else{ DocLnk dc; dc.setFile(filename); dc.setType("text/plain"); openFile(dc); } } else { openFile(DocLnk(filename)); } showEditTools(); updateCaption( filename ); if ( msg == "viewFile(QString)" ) setReadOnly(TRUE); QPEApplication::setKeepRunning(); }}void TextEdit::setReadOnly(bool y){ editor->setReadOnly(y); if ( y ) editor->setEdited(FALSE);}#include "textedit.moc"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -