KDevelop API Documentation

markerwidget.cpp

Go to the documentation of this file.
00001 /* $Id: markerwidget.cpp,v 1.19 2003/05/20 23:05:42 jb Exp $
00002  *
00003  *  Copyright (C) 2002 Roberto Raggi (roberto@kdevelop.org)
00004  *
00005  *  This program is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2 of the License, or (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; see the file COPYING.  If not, write to
00017  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  *  Boston, MA 02111-1307, USA.
00019  *
00020  */
00021 
00022 /**********************************************************************
00023 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00024 **
00025 ** This file is part of Qt Designer.
00026 **
00027 ** This file may be distributed and/or modified under the terms of the
00028 ** GNU General Public License version 2 as published by the Free Software
00029 ** Foundation and appearing in the file COPYING included in the
00030 ** packaging of this file.
00031 **
00032 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00033 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00034 **
00035 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00036 **
00037 ** Contact info@trolltech.com if any conditions of this licensing are
00038 ** not clear to you.
00039 **
00040 **********************************************************************/
00041 
00042 #include "markerwidget.h"
00043 #include "qeditor.h"
00044 #include "paragdata.h"
00045 
00046 #include <qpopupmenu.h>
00047 #include <private/qrichtext_p.h>
00048 #include <kdebug.h>
00049 #include <kiconloader.h>
00050 #include <klocale.h>
00051 
00052 using namespace std;
00053 
00054 MarkerWidget::MarkerWidget( QEditor* editor, QWidget* parent, const char* name )
00055     : QWidget( parent, name, WRepaintNoErase | WStaticContents | WResizeNoErase ),
00056       m_editor( editor )
00057       ,m_clickChangesBPs(true)
00058       ,m_changeBookmarksAllowed(false)
00059       ,m_changeBreakpointsAllowed(false)
00060       ,m_bookmarkDescr(i18n("Bookmark"))
00061       ,m_breakpointDescr(i18n("Breakpoint"))
00062 {
00063     m_pixmapMap.insert(0x01, SmallIcon("attach"));
00064     m_pixmapMap.insert(0x05, SmallIcon("exec"));
00065     m_pixmapMap.insert(0x200, SmallIcon("stop"));
00066     m_pixmapMap.insert(0x400, SmallIcon("fun"));
00067 
00068     setFixedWidth( 20 );
00069 
00070     connect( m_editor->verticalScrollBar(), SIGNAL( valueChanged( int ) ),
00071              this, SLOT( doRepaint() ) );
00072     connect( m_editor, SIGNAL( textChanged() ),
00073              this, SLOT( doRepaint() ) );
00074 
00075     doRepaint();
00076 }
00077 
00078 MarkerWidget::~MarkerWidget()
00079 {
00080 }
00081 
00082 void MarkerWidget::paintEvent( QPaintEvent* /*e*/ )
00083 {
00084     m_buffer.fill();
00085 
00086     QTextParagraph *p = m_editor->document()->firstParagraph();
00087     QPainter painter( &m_buffer );
00088     int yOffset = m_editor->contentsY();
00089     while ( p ) {
00090         if ( !p->isVisible() ) {
00091             p = p->next();
00092             continue;
00093         }
00094         if ( p->rect().y() + p->rect().height() - yOffset < 0 ) {
00095             p = p->next();
00096             continue;
00097         }
00098         if ( p->rect().y() - yOffset > height() )
00099             break;
00100 
00101         ParagData* paragData = (ParagData*) p->extraData();
00102         unsigned int mark = paragData ? paragData->mark() : 0;
00103         if (mark) {
00104             unsigned int current = 0x01;
00105             for (; current < mark+1; current = current << 1) {
00106                 if (mark & current) {
00107                     QMapIterator<int,QPixmap> it = m_pixmapMap.find(current);
00108                     if (it != m_pixmapMap.end()) {
00109                         painter.drawPixmap( 3,
00110                                             p->rect().y() + ( p->rect().height() - (*it).height() ) / 2 - yOffset,
00111                                             *it );
00112                     }
00113                 }
00114             }
00115         }
00116         p = p->next();
00117     }
00118 
00119     painter.end();
00120     bitBlt( this, 0, 0, &m_buffer );
00121 }
00122 
00123 
00124 void MarkerWidget::resizeEvent( QResizeEvent *e )
00125 {
00126     m_buffer.resize( e->size() );
00127     QWidget::resizeEvent( e );
00128 }
00129 
00130 void MarkerWidget::contextMenuEvent( QContextMenuEvent* e )
00131 {
00132     QPopupMenu m( 0, "editor_breakpointsmenu" );
00133     QPopupMenu sub( 0, "editor_breakpointsmenu_sub" );
00134 
00135     int toggleBreakpoint = 0;
00136     int toggleBookmark = 0;
00137     int lmbClickChangesBPs = 0;
00138     int lmbClickChangesBookmarks = 0;
00139 
00140     QTextParagraph *p = m_editor->document()->firstParagraph();
00141     int yOffset = m_editor->contentsY();
00142     while ( p ) {
00143         if ( e->y() >= p->rect().y() - yOffset && e->y() <= p->rect().y() + p->rect().height() - yOffset ) {
00144             ParagData* data = (ParagData*) p->extraData();
00145         
00146             toggleBookmark = m.insertItem( m_bookmarkDescr );
00147             m.setItemEnabled(toggleBookmark, m_changeBookmarksAllowed);
00148             m.setItemChecked(toggleBookmark, data->mark() & 0x01);
00149         
00150             toggleBreakpoint = m.insertItem( m_breakpointDescr );
00151             m.setItemEnabled(toggleBreakpoint, m_changeBreakpointsAllowed);
00152             m.setItemChecked(toggleBreakpoint, data->mark() & 0x02);
00153         
00154         m.insertItem("Set default mark type", &sub);
00155             lmbClickChangesBookmarks = sub.insertItem( m_bookmarkDescr );
00156             lmbClickChangesBPs = sub.insertItem( m_breakpointDescr );
00157             m.setItemChecked(lmbClickChangesBPs, m_clickChangesBPs);
00158             m.setItemChecked(lmbClickChangesBookmarks, !m_clickChangesBPs);
00159             break;
00160         }
00161         p = p->next();
00162     }
00163 
00164     int res = m.exec( e->globalPos() );
00165     if ( res == -1)
00166         return;
00167 
00168     ParagData* data = (ParagData*) p->extraData();
00169 
00170     KTextEditor::Mark mark;
00171     mark.line = p->paragId();
00172 
00173     if ( res == toggleBookmark && m_changeBookmarksAllowed ) {
00174         mark.type = 0x01;
00175         if ( data->mark() & 0x01 ) {
00176             data->setMark( data->mark() & ~0x01 );
00177             emit markChanged(mark, KTextEditor::MarkInterfaceExtension::MarkRemoved);
00178         }
00179         else {
00180             data->setMark( data->mark() | 0x01 );
00181             emit markChanged(mark, KTextEditor::MarkInterfaceExtension::MarkAdded);
00182         }
00183     } else if ( res == toggleBreakpoint && m_changeBreakpointsAllowed ) {
00184         mark.type = 0x02;
00185         if ( data->mark() & 0x02 ) {
00186             data->setMark( data->mark() & ~0x02 );
00187             emit markChanged(mark, KTextEditor::MarkInterfaceExtension::MarkRemoved);
00188         }
00189         else {
00190             data->setMark( data->mark() | 0x02 );
00191             emit markChanged(mark, KTextEditor::MarkInterfaceExtension::MarkAdded);
00192         }
00193     } else if ( res == lmbClickChangesBPs ) {
00194         m_clickChangesBPs = !m.isItemChecked(lmbClickChangesBPs);
00195     } else if ( res == lmbClickChangesBookmarks ) {
00196         m_clickChangesBPs = m.isItemChecked(lmbClickChangesBookmarks);
00197     }
00198 
00199     emit marksChanged();
00200     doRepaint();
00201 }
00202 
00203 void MarkerWidget::mousePressEvent( QMouseEvent * e )
00204 {
00205   QTextParagraph *p = m_editor->document()->firstParagraph();
00206   int yOffset = m_editor->contentsY();
00207   ParagData* data = 0L;
00208   while ( p ) {
00209     if ( e->y() >= p->rect().y() - yOffset && e->y() <= p->rect().y() + p->rect().height() - yOffset ) {
00210       data = (ParagData*) p->extraData();
00211       break;
00212     }
00213     p = p->next();
00214   }
00215 
00216   if (e->button() == Qt::LeftButton) {
00217     if (!data) return;
00218 
00219     KTextEditor::Mark mark;
00220     mark.line = p->paragId();
00221     if (m_clickChangesBPs && m_changeBreakpointsAllowed) {
00222       mark.type = 0x02;
00223       if (data->mark() & 0x02) {
00224         data->setMark(data->mark() & ~0x02);
00225         emit markChanged(mark, KTextEditor::MarkInterfaceExtension::MarkRemoved);
00226       }
00227       else {
00228         data->setMark(data->mark() | 0x02);
00229         emit markChanged(mark, KTextEditor::MarkInterfaceExtension::MarkAdded);
00230       }
00231     }
00232     else if (m_changeBookmarksAllowed) {
00233       mark.type = 0x01;
00234       if (data->mark() & 0x01) {
00235         data->setMark(data->mark() & ~0x01);
00236         emit markChanged(mark, KTextEditor::MarkInterfaceExtension::MarkRemoved);
00237       }
00238       else {
00239         data->setMark(data->mark() | 0x01);
00240         emit markChanged(mark, KTextEditor::MarkInterfaceExtension::MarkAdded);
00241       }
00242     }
00243   }
00244 
00245   emit marksChanged();
00246   doRepaint();
00247 }
00248 
00249 void MarkerWidget::setPixmap(KTextEditor::MarkInterface::MarkTypes mt, const QPixmap & pm)
00250 {
00251   if (mt)
00252     m_pixmapMap.insert(mt, pm);
00253 }
00254 
00255 void MarkerWidget::setDescription(KTextEditor::MarkInterface::MarkTypes mt, const QString & s)
00256 {
00257   switch (mt) {
00258   case KTextEditor::MarkInterface::markType01: m_bookmarkDescr = s; break;
00259   case KTextEditor::MarkInterface::markType02: m_breakpointDescr = s; break;
00260   default: break;
00261   }
00262 }
00263 
00264 void MarkerWidget::setMarksUserChangable(uint markMask)
00265 {
00266   m_changeBookmarksAllowed   = (markMask & KTextEditor::MarkInterface::markType01) ? true : false;
00267   m_changeBreakpointsAllowed = (markMask & KTextEditor::MarkInterface::markType02) ? true : false;
00268 
00269   doRepaint();
00270 }
00271 
00272 #include "markerwidget.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 00:03:42 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003