umbrello API Documentation

notewidget.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  *   copyright (C) 2002-2006                                               *
00009  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00010  ***************************************************************************/
00011 
00012 // own header
00013 #include "notewidget.h"
00014 //qt includes
00015 #include <qpointarray.h>
00016 #include <qpainter.h>
00017 #include <qtextedit.h>
00018 #include <qframe.h>
00019 // kde includes
00020 #include <kdebug.h>
00021 #include <kcolordialog.h>
00022 // app includes
00023 #include "notewidgetcontroller.h"
00024 #include "dialogs/notedialog.h"
00025 #include "clipboard/umldrag.h"
00026 #include "umldoc.h"
00027 #include "umlview.h"
00028 #include "uml.h"
00029 #include "listpopupmenu.h"
00030 
00031 #define NOTEMARGIN 10
00032 
00033 NoteWidget::NoteWidget(UMLView * view, Uml::IDType id)
00034         : UMLWidget(view, id, new NoteWidgetController(this)) {
00035     init();
00036     setSize(100,80);
00037     setZ( 20 ); //make sure always on top.
00038 #ifdef NOTEWIDGET_EMBED_EDITOR
00039     // NB: This code is currently deactivated because
00040     // Zoom does not yet work with the embedded text editor.
00041     m_pEditor = new QTextEdit(view);
00042     m_pEditor->setFrameStyle(QFrame::NoFrame | QFrame::Plain);
00043     m_pEditor->setHScrollBarMode(QScrollView::AlwaysOff);
00044     m_pEditor->setVScrollBarMode(QScrollView::AlwaysOff);
00045     m_pEditor->setTextFormat(Qt::RichText);
00046     m_pEditor->setShown(true);
00047     setEditorGeometry();
00048     connect(m_pView, SIGNAL(contentsMoving(int, int)),
00049             this, SLOT(slotViewScrolled(int, int)));
00050 #endif
00051 }
00052 
00053 void NoteWidget::init() {
00054     UMLWidget::setBaseType(Uml::wt_Note);
00055     m_DiagramLink = Uml::id_None;
00056 }
00057 
00058 NoteWidget::~NoteWidget() {
00059 #ifdef NOTEWIDGET_EMBED_EDITOR
00060     delete m_pEditor;
00061 #endif
00062 }
00063 
00064 void NoteWidget::setDiagramLink(Uml::IDType viewID) {
00065     UMLDoc *umldoc = UMLApp::app()->getDocument();
00066     UMLView *view = umldoc->findView(viewID);
00067     if (view == NULL) {
00068         kError() << "NoteWidget::setDiagramLink(" << ID2STR(viewID)
00069         << "): no view found for this ID." << endl;
00070         return;
00071     }
00072     QString linkText("Diagram: " + view->getName());
00073 #if defined (NOTEWIDGET_EMBED_EDITOR)
00074     m_pEditor->setUnderline(true);
00075     m_pEditor->insert(linkText);
00076     m_pEditor->setUnderline(false);
00077 #else
00078     setDoc(linkText);
00079     update();
00080 #endif
00081     m_DiagramLink = viewID;
00082 }
00083 
00084 Uml::IDType NoteWidget::getDiagramLink() const {
00085     return m_DiagramLink;
00086 }
00087 
00088 void NoteWidget::slotViewScrolled(int x, int y) {
00089     setEditorGeometry(x, y);
00090 }
00091 
00092 void NoteWidget::setFont(QFont font) {
00093     UMLWidget::setFont(font);
00094 #ifdef NOTEWIDGET_EMBED_EDITOR
00095     m_pEditor->setFont(font);
00096 #endif
00097 }
00098 
00099 void NoteWidget::setEditorGeometry(int dx /*=0*/, int dy /*=0*/) {
00100 #if defined (NOTEWIDGET_EMBED_EDITOR)
00101     const QRect editorGeometry( UMLWidget::getX() - dx + 6,
00102                                 UMLWidget::getY() - dy + 10,
00103                                 UMLWidget::getWidth() - 16,
00104                                 UMLWidget::getHeight() - 16);
00105     m_pEditor->setGeometry( editorGeometry );
00106     drawText();
00107 #else
00108     dx=0; dy=0;   // avoid "unused arg" warnings
00109 #endif
00110 }
00111 
00112 void NoteWidget::setX( int x ) {
00113     UMLWidget::setX(x);
00114     setEditorGeometry();
00115 }
00116 
00117 void NoteWidget::setY( int y ) {
00118     UMLWidget::setY(y);
00119     setEditorGeometry();
00120 }
00121 
00122 QString NoteWidget::getDoc() const {
00123 #if defined (NOTEWIDGET_EMBED_EDITOR)
00124     return m_pEditor->text();
00125 #else
00126     return m_Text;
00127 #endif
00128 }
00129 
00130 void NoteWidget::setDoc(const QString &newText) {
00131 #if defined (NOTEWIDGET_EMBED_EDITOR)
00132     m_pEditor->setText(newText);
00133 #else
00134     m_Text = newText;
00135 #endif
00136 }
00137 
00138 void NoteWidget::draw(QPainter & p, int offsetX, int offsetY) {
00139     int margin = 10;
00140     int w = width()-1;
00141 
00142     int h= height()-1;
00143     QPointArray poly(6);
00144     poly.setPoint(0, offsetX, offsetY);
00145     poly.setPoint(1, offsetX, offsetY + h);
00146     poly.setPoint(2, offsetX + w, offsetY + h);
00147     poly.setPoint(3, offsetX + w, offsetY + margin);
00148     poly.setPoint(4, offsetX + w - margin, offsetY);
00149     poly.setPoint(5, offsetX, offsetY);
00150     UMLWidget::setPen(p);
00151     if ( UMLWidget::getUseFillColour() ) {
00152         QBrush brush( UMLWidget::getFillColour() );
00153         p.setBrush(brush);
00154         p.drawPolygon(poly);
00155 #if defined (NOTEWIDGET_EMBED_EDITOR)
00156         m_pEditor->setPaper(brush);
00157 #endif
00158     } else
00159         p.drawPolyline(poly);
00160     p.drawLine(offsetX + w - margin, offsetY, offsetX + w - margin, offsetY + margin);
00161     p.drawLine(offsetX + w - margin, offsetY + margin, offsetX + w, offsetY + margin);
00162     if(m_bSelected) {
00163         drawSelected(&p, offsetX, offsetY);
00164     }
00165 
00166     drawText(&p, offsetX, offsetY);
00167 }
00168 
00169 QSize NoteWidget::calculateSize() {
00170     return QSize(50, 50);
00171 }
00172 
00173 void NoteWidget::slotMenuSelection(int sel) {
00174     NoteDialog * dlg = 0;
00175     UMLDoc *doc = UMLApp::app()->getDocument();
00176     switch(sel) {
00178         // case ListPopupMenu::mt_Link_Docs:
00179         //      m_pView->updateNoteWidgets();
00180         //      doc -> setModified(true);
00181         //      break;
00182 
00183     case ListPopupMenu::mt_Rename:
00184         m_pView -> updateDocumentation( false );
00185         dlg = new NoteDialog( m_pView, this );
00186         if( dlg -> exec() ) {
00187             m_pView -> showDocumentation( this, true );
00188             doc -> setModified(true);
00189             update();
00190         }
00191         delete dlg;
00192         break;
00193 
00194     default:
00195         UMLWidget::slotMenuSelection(sel);
00196         break;
00197     }
00198 }
00199 
00200 void NoteWidget::drawText(QPainter * p /*=NULL*/, int offsetX /*=0*/, int offsetY /*=0*/) {
00201 #if defined (NOTEWIDGET_EMBED_EDITOR)
00202     m_pEditor->setText( getDoc() );
00203     m_pEditor->setShown(true);
00204     m_pEditor->repaint();
00205 #else
00206     if (p == NULL)
00207         return;
00208     /*
00209     Implement word wrap for text as follows:
00210     wrap at width on whole words.
00211     if word is wider than width then clip word
00212     if reach height exit and don't print anymore
00213     start new line on \n character
00214     */
00215     p->setPen( Qt::black );
00216     QFont font = UMLWidget::getFont();
00217     p->setFont( font );
00218     const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
00219     const int fontHeight  = fm.lineSpacing();
00220     QString text = getDoc();
00221     if( text.length() == 0 )
00222         return;
00223     QString word = "";
00224     QString fullLine = "";
00225     QString testCombineLine = "";
00226     const int margin = fm.width( "W" );
00227     int textY = fontHeight / 2;
00228     int textX = margin;
00229     const int width = this -> width() - margin * 2;
00230     const int height = this -> height() - fontHeight;
00231     QChar returnChar('\n');
00232     QChar c;
00233     for (uint i = 0; i <= text.length(); i++) {
00234         if (i < text.length()) {
00235             c = text[i];
00236         } else {
00237             // all chars of text have been handled already ->
00238             // perform this last run to spool current content of "word"
00239             c = returnChar;
00240         }
00241         if (c == returnChar || c.isSpace()) {
00242             // new word delimiter found -> its time to decide on word wrap
00243             testCombineLine = fullLine + ' ' + word;
00244             int textWidth = fm.width( testCombineLine );
00245             if (textX + textWidth > width) {
00246                 // combination of "fullLine" and "word" doesn't fit into one line ->
00247                 // print "fullLine" in current line, update write position to next line
00248                 // and decide then on following actions
00249                 p->drawText(offsetX + textX, offsetY + textY,
00250                             textWidth, fontHeight, Qt::AlignLeft, fullLine );
00251                 fullLine = word;
00252                 word = "";
00253                 // update write position
00254                 textX = margin;
00255                 textY += fontHeight;
00256                 if (textY > height)
00257                     return;
00258                 // in case of c==newline ->
00259                 // print "word" and set write position one additional line lower
00260                 if (c == returnChar) {
00261                     // print "word" - which is now "fullLine" and set to next line
00262                     p->drawText(offsetX + textX, offsetY + textY,
00263                                 textWidth, fontHeight, Qt::AlignLeft, fullLine);
00264                     fullLine = "";
00265                     textX = margin;
00266                     textY += fontHeight;
00267                     if( textY > height ) return;
00268                 }
00269             }
00270             else if ( c == returnChar ) {
00271                 // newline found and combination of "fullLine" and "word" fits
00272                 // in one line
00273                 p->drawText(offsetX + textX, offsetY + textY,
00274                             textWidth, fontHeight, Qt::AlignLeft, testCombineLine);
00275                 fullLine = word = "";
00276                 textX = margin;
00277                 textY += fontHeight;
00278                 if (textY > height)
00279                     return;
00280             } else {
00281                 // word delimiter found, and combination of "fullLine", space and "word" fits into one line
00282                 fullLine = testCombineLine;
00283                 word = "";
00284             }
00285         } else {
00286             // no word delimiter found --> add current char to "word"
00287             if (c != '\0')
00288                 word += c;
00289         }
00290     }//end for
00291 #endif
00292 }
00293 
00294 void NoteWidget::saveToXMI( QDomDocument & qDoc, QDomElement & qElement ) {
00295     QDomElement noteElement = qDoc.createElement( "notewidget" );
00296     UMLWidget::saveToXMI( qDoc, noteElement );
00297     noteElement.setAttribute( "text", getDoc() );
00298     if (m_DiagramLink != Uml::id_None)
00299         noteElement.setAttribute( "diagramlink", ID2STR(m_DiagramLink) );
00300     qElement.appendChild( noteElement );
00301 }
00302 
00303 bool NoteWidget::loadFromXMI( QDomElement & qElement ) {
00304     if( !UMLWidget::loadFromXMI( qElement ) )
00305         return false;
00306     setZ( 20 ); //make sure always on top.
00307     setDoc( qElement.attribute("text", "") );
00308     QString diagramlink = qElement.attribute("diagramlink", "");
00309     if (!diagramlink.isEmpty())
00310         m_DiagramLink = STR2ID(diagramlink);
00311     return true;
00312 }
00313 
00314 
00315 #include "notewidget.moc"
00316 
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:07:58 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003