• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

kjots

kjotsedit.cpp

Go to the documentation of this file.
00001 //
00002 //  kjots
00003 //
00004 //  Copyright (C) 1997 Christoph Neerfeld <Christoph.Neerfeld@home.ivm.de>
00005 //  Copyright (C) 2002, 2003 Aaron J. Seigo <aseigo@kde.org>
00006 //  Copyright (C) 2003 Stanislav Kljuhhin <crz@hot.ee>
00007 //  Copyright (C) 2005-2006 Jaison Lee <lee.jaison@gmail.com>
00008 //  Copyright (C) 2007 Stephen Kelly <steveire@gmail.com>
00009 //
00010 //  This program is free software; you can redistribute it and/or modify
00011 //  it under the terms of the GNU General Public License as published by
00012 //  the Free Software Foundation; either version 2 of the License, or
00013 //  (at your option) any later version.
00014 //
00015 //  This program is distributed in the hope that it will be useful,
00016 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //  GNU General Public License for more details.
00019 //
00020 //  You should have received a copy of the GNU General Public License
00021 //  along with this program; if not, write to the Free Software
00022 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023 //
00024 
00025 //Own Header
00026 #include "kjotsedit.h"
00027 
00028 #include <QMimeData>
00029 #include <QTextCursor>
00030 #include <QStackedWidget>
00031 #include <QUrl>
00032 #include <QMenu>
00033 #include <QContextMenuEvent>
00034 
00035 #include <kaction.h>
00036 #include <kactioncollection.h>
00037 #include <kfontdialog.h>
00038 
00039 #include "kjotsentry.h"
00040 #include "bookshelf.h"
00041 
00042 KJotsEdit::KJotsEdit ( QWidget *parent ) : KTextEdit(parent)
00043 {
00044     setAcceptRichText(true);
00045     setWordWrapMode(QTextOption::WordWrap);
00046 }
00047 
00048 KJotsEdit::~KJotsEdit()
00049 {
00050 }
00051 
00052 void KJotsEdit::contextMenuEvent( QContextMenuEvent *event )
00053 {
00054     QMenu *popup = createStandardContextMenu();
00055     connect( popup, SIGNAL( triggered ( QAction* ) ),
00056              this, SLOT( menuActivated( QAction* ) ) );
00057 
00058     popup->addSeparator();
00059     popup->addAction(actionCollection->action("change_font"));
00060     popup->addAction(actionCollection->action("copyIntoTitle"));
00061 
00062     popup->exec( event->globalPos() );
00063 
00064     delete popup;
00065 }
00066 
00067 void KJotsEdit::DelayedInitialization ( KActionCollection *collection, Bookshelf *shelf )
00068 {
00069     bookshelf = shelf;
00070     actionCollection = collection;
00071 
00072     connect(actionCollection->action("italics"), SIGNAL(triggered()), SLOT(onItalics()));
00073     connect(actionCollection->action("underline"), SIGNAL(triggered()), SLOT(onUnderline()));
00074     connect(actionCollection->action("bold"), SIGNAL(triggered()), SLOT(onBold()));
00075     connect(actionCollection->action("strike_out"), SIGNAL(triggered()), SLOT(onStrikeOut()));
00076     connect(actionCollection->action("linkify"), SIGNAL(triggered()), SLOT(onLinkify()));
00077 
00078     connect(actionCollection->action("auto_bullet"), SIGNAL(triggered()), SLOT(onAutoBullet()));
00079     connect(actionCollection->action("horizontal_rule"), SIGNAL(triggered()), SLOT(onRule()));
00080     connect(actionCollection->action("change_font"), SIGNAL(triggered()), SLOT(onFonts()));
00081 
00082     connect(bookshelf, SIGNAL(itemSelectionChanged()), SLOT(onBookshelfSelection()));
00083     connect(this, SIGNAL(selectionChanged()), SLOT(onSelectionChanged()));
00084     connect(this, SIGNAL(textChanged()), SLOT(onTextChanged()));
00085 
00086     connect(this, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)),
00087             this, SLOT(updateCurrentCharFormat(const QTextCharFormat &)));
00088 
00089 }
00090 
00091 void KJotsEdit::disableEditing ( void )
00092 {
00093     if ( currentPage ) {
00094         currentPage->setCursor(textCursor());
00095         setDocument(0);
00096         currentPage = 0;
00097     }
00098 
00099     setReadOnly(true);
00100     setEnabled(false);
00101 }
00102 
00103 void KJotsEdit::onBookshelfSelection ( void )
00104 {
00105     QList<QTreeWidgetItem*> selection = bookshelf->selectedItems();
00106     int selectionSize = selection.size();
00107 
00108     if (selectionSize !=  1) {
00109         disableEditing();
00110     } else {
00111         KJotsPage *newPage = dynamic_cast<KJotsPage*>(selection[0]);
00112         if ( !newPage ) {
00113             disableEditing();
00114         } else {
00115             if ( currentPage != newPage ) {
00116                 if ( currentPage ) {
00117                     currentPage->setCursor(textCursor());
00118                 }
00119                 currentPage = newPage;
00120 
00121                 setEnabled(true);
00122                 setReadOnly(false);
00123                 setDocument(currentPage->body());
00124                 if ( !currentPage->getCursor().isNull() ) {
00125                     setTextCursor(currentPage->getCursor());
00126                 }
00127 
00128                 QStackedWidget *stack = static_cast<QStackedWidget*>(parent());
00129                 stack->setCurrentWidget(this);
00130                 setFocus();
00131 
00132                 if ( textCursor().atStart() )
00133                 {
00134                     // Reflect formatting when switching pages and the first word is formatted
00135                     // Work-around for qrextedit bug. The format does not seem to exist
00136                     // before the first character. Submitted to qt-bugs
00137                     moveCursor(QTextCursor::Right);
00138                     moveCursor(QTextCursor::Left);
00139                 }
00140 
00141             }
00142         }
00143     }
00144 }
00145 
00146 void KJotsEdit::onItalics ( void )
00147 {
00148     QTextCharFormat fmt;
00149     fmt.setFontItalic(actionCollection->action("italics")->isChecked());
00150     mergeFormatOnWordOrSelection(fmt);
00151 }
00152 
00153 void KJotsEdit::onUnderline ( void )
00154 {
00155     QTextCharFormat fmt;
00156     fmt.setFontUnderline(actionCollection->action("underline")->isChecked());
00157     mergeFormatOnWordOrSelection(fmt);
00158 }
00159 
00160 void KJotsEdit::onBold ( void )
00161 {
00162     QTextCharFormat fmt;
00163     fmt.setFontWeight(actionCollection->action("bold")->isChecked() ? QFont::Bold : QFont::Normal);
00164     mergeFormatOnWordOrSelection(fmt);
00165 }
00166 
00167 void KJotsEdit::onStrikeOut ( void )
00168 {
00169     QTextCharFormat fmt;
00170     fmt.setFontStrikeOut(actionCollection->action("strike_out")->isChecked() ? true : false);
00171     mergeFormatOnWordOrSelection(fmt);
00172 }
00173 
00174 void KJotsEdit::updateCurrentCharFormat(const QTextCharFormat &format)
00175 {
00176     fontChanged(format.font());
00177 }
00178 
00179 void KJotsEdit::fontChanged(const QFont &f)
00180 {
00181     actionCollection->action("italics")->setChecked(f.italic());
00182     actionCollection->action("bold")->setChecked(f.bold());
00183     actionCollection->action("underline")->setChecked(f.underline());
00184     actionCollection->action("strike_out")->setChecked(f.strikeOut());
00185 }
00186 
00187 void KJotsEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
00188 {
00189     QTextCursor cursor = this->textCursor();
00190     if (!cursor.hasSelection())
00191         cursor.select(QTextCursor::WordUnderCursor);
00192     cursor.mergeCharFormat(format);
00193     this->mergeCurrentCharFormat(format);
00194 }
00195 
00196 void KJotsEdit::onAutoBullet ( void )
00197 {
00198     KTextEdit::AutoFormatting currentFormatting = autoFormatting();
00199 
00200     if ( currentFormatting == KTextEdit::AutoBulletList ) {
00201         setAutoFormatting(KTextEdit::AutoNone);
00202         actionCollection->action("auto_bullet")->setChecked( false );
00203     } else {
00204         setAutoFormatting(KTextEdit::AutoBulletList);
00205         actionCollection->action("auto_bullet")->setChecked( true );
00206     }
00207 }
00208 
00209 void KJotsEdit::onRule ( void )
00210 {
00211     QTextCursor c = textCursor();
00212     QTextBlockFormat bf = c.blockFormat();
00213     QTextCharFormat cf = c.charFormat();
00214     c.insertHtml("<hr>");
00215     c.insertBlock(bf, cf);
00216     setTextCursor(c);
00217 }
00218 
00219 void KJotsEdit::onFonts ( void )
00220 {
00221     QFont newFont;
00222     int result = KFontDialog::getFont( newFont );
00223     if ( result == KFontDialog::Accepted ) {
00224         setCurrentFont(newFont);
00225     }
00226 }
00227 
00228 void KJotsEdit::onLinkify ( void )
00229 {
00230     QTextCursor selectionCursor = textCursor();
00231     Q_ASSERT(selectionCursor.hasSelection());
00232 
00233     QString linkified = QString("<a href='%1'>%2</a> ")
00234         .arg(selectionCursor.selectedText()).arg(selectionCursor.selectedText());
00235 
00236     insertHtml(linkified);
00237 }
00238 
00239 void KJotsEdit::onSelectionChanged ( void )
00240 {
00241     QTextCursor selectionCursor = textCursor();
00242     if ( selectionCursor.hasSelection() ) {
00243         QUrl selectedUrl ( selectionCursor.selectedText() );
00244         if ( !selectedUrl.isEmpty() && selectedUrl.isValid() ) {
00245             actionCollection->action("linkify")->setEnabled(true);
00246             return;
00247         }
00248     }
00249 
00250     actionCollection->action("linkify")->setEnabled(false);
00251 }
00252 
00253 void KJotsEdit::onTextChanged ( void )
00254 {
00255     if ( currentPage ) {
00256         currentPage->parentBook()->setDirty(true);
00257     }
00258 }
00259 
00260 bool KJotsEdit::canInsertFromMimeData ( const QMimeData *source ) const
00261 {
00262     if ( source->formats().contains("kjots/internal_link") ) {
00263         return true;
00264     } else if ( source->hasUrls() ) {
00265         return true;
00266     } else {
00267         return KTextEdit::canInsertFromMimeData(source);
00268     }
00269 }
00270 
00271 void KJotsEdit::insertFromMimeData ( const QMimeData *source )
00272 {
00273     if ( source->formats().contains("kjots/internal_link") ) {
00274         insertHtml(source->data("kjots/internal_link"));
00275     } else if ( source->hasUrls() ) {
00276         foreach ( QUrl url, source->urls() ) {
00277             if ( url.isValid() ) {
00278                 QString html = QString ( "<a href='%1'>%2</a> " )
00279                     .arg(QString::fromUtf8(url.toEncoded()))
00280                     .arg(url.toString(QUrl::RemovePassword));
00281                 insertHtml(html);
00282             }
00283         }
00284     } else {
00285         KTextEdit::insertFromMimeData(source);
00286     }
00287 }
00288 
00289 #include "kjotsedit.moc"
00290 /* ex: set tabstop=4 softtabstop=4 shiftwidth=4 expandtab: */
00291 /* kate: tab-indents off; replace-tabs on; tab-width 4; remove-trailing-space on; encoding utf-8;*/

kjots

Skip menu "kjots"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdelirc
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • kjots
  • klaptopdaemon
  • kmilo
  • ksim
  • ktimer
  • kwallet
  • superkaramba
Generated for kdeutils by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal