compile
[kdegraphics.git] / okular / ui / annotwindow.cpp
blob52941d4ff92fec6cdf8f2a1e050f52a123a5f614
1 /***************************************************************************
2 * Copyright (C) 2006 by Chu Xiaodong <xiaodongchu@gmail.com> *
3 * Copyright (C) 2006 by Pino Toscano <pino@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
11 #include "annotwindow.h"
13 // qt/kde includes
14 #include <qapplication.h>
15 #include <qevent.h>
16 #include <qfont.h>
17 #include <qfontinfo.h>
18 #include <qfontmetrics.h>
19 #include <qframe.h>
20 #include <qlabel.h>
21 #include <qlayout.h>
22 #include <QPainter>
23 #include <qpushbutton.h>
24 #include <qsizegrip.h>
25 #include <qstyle.h>
26 #include <qtoolbutton.h>
27 #include <kglobal.h>
28 #include <klocale.h>
29 #include <ktextedit.h>
30 #include <kdebug.h>
32 // local includes
33 #include "core/annotations.h"
34 #include "guiutils.h"
36 class CloseButton
37 : public QPushButton
39 public:
40 CloseButton( QWidget * parent = 0 )
41 : QPushButton( parent )
43 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
44 QSize size = QSize( 14, 14 ).expandedTo( QApplication::globalStrut() );
45 setFixedSize( size );
46 setIcon( style()->standardIcon( QStyle::SP_DockWidgetCloseButton ) );
47 setIconSize( size );
48 setToolTip( i18n( "Close this note" ) );
53 class MovableTitle
54 : public QWidget
56 public:
57 MovableTitle( QWidget * parent )
58 : QWidget( parent )
60 QVBoxLayout * mainlay = new QVBoxLayout( this );
61 mainlay->setMargin( 0 );
62 mainlay->setSpacing( 0 );
63 // close button row
64 QHBoxLayout * buttonlay = new QHBoxLayout();
65 mainlay->addLayout( buttonlay );
66 titleLabel = new QLabel( this );
67 QFont f = titleLabel->font();
68 f.setBold( true );
69 titleLabel->setFont( f );
70 titleLabel->setCursor( Qt::SizeAllCursor );
71 buttonlay->addWidget( titleLabel );
72 dateLabel = new QLabel( this );
73 dateLabel->setAlignment( Qt::AlignTop | Qt::AlignRight );
74 f = dateLabel->font();
75 f.setPointSize( QFontInfo( f ).pointSize() - 2 );
76 dateLabel->setFont( f );
77 dateLabel->setCursor( Qt::SizeAllCursor );
78 buttonlay->addWidget( dateLabel );
79 CloseButton * close = new CloseButton( this );
80 connect( close, SIGNAL( clicked() ), parent, SLOT( hide() ) );
81 buttonlay->addWidget( close );
82 // option button row
83 QHBoxLayout * optionlay = new QHBoxLayout();
84 mainlay->addLayout( optionlay );
85 authorLabel = new QLabel( this );
86 authorLabel->setCursor( Qt::SizeAllCursor );
87 authorLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
88 optionlay->addWidget( authorLabel );
89 optionButton = new QToolButton( this );
90 QString opttext = i18n( "Options" );
91 optionButton->setText( opttext );
92 optionButton->setAutoRaise( true );
93 QSize s = QFontMetrics( optionButton->font() ).boundingRect( opttext ).size() + QSize( 8, 8 );
94 optionButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
95 optionButton->setFixedSize( s );
96 optionlay->addWidget( optionButton );
97 // ### disabled for now
98 optionButton->hide();
100 titleLabel->installEventFilter( this );
101 dateLabel->installEventFilter( this );
102 authorLabel->installEventFilter( this );
105 virtual bool eventFilter( QObject * obj, QEvent * e )
107 if ( obj != titleLabel && obj != authorLabel && obj != dateLabel )
108 return false;
110 QMouseEvent * me = 0;
111 switch ( e->type() )
113 case QEvent::MouseButtonPress:
114 me = (QMouseEvent*)e;
115 mousePressPos = me->pos();
116 break;
117 case QEvent::MouseButtonRelease:
118 mousePressPos = QPoint();
119 break;
120 case QEvent::MouseMove:
121 me = (QMouseEvent*)e;
122 parentWidget()->move( me->pos() - mousePressPos + parentWidget()->pos() );
123 break;
124 default:
125 return false;
127 return true;
130 void setTitle( const QString& title )
132 titleLabel->setText( QString( " " ) + title );
135 void setDate( const QDateTime& dt )
137 dateLabel->setText( KGlobal::locale()->formatDateTime( dt, KLocale::ShortDate, true ) + ' ' );
140 void setAuthor( const QString& author )
142 authorLabel->setText( QString( " " ) + author );
145 void connectOptionButton( QObject * recv, const char* method )
147 connect( optionButton, SIGNAL( clicked() ), recv, method );
150 private:
151 QLabel * titleLabel;
152 QLabel * dateLabel;
153 QLabel * authorLabel;
154 QPoint mousePressPos;
155 QToolButton * optionButton;
159 // Qt::SubWindow is needed to make QSizeGrip work
160 AnnotWindow::AnnotWindow( QWidget * parent, Okular::Annotation * annot)
161 : QFrame( parent, Qt::SubWindow ), m_annot( annot )
163 setAutoFillBackground( true );
164 setFrameStyle( Panel | Raised );
166 textEdit = new KTextEdit( this );
167 textEdit->setAcceptRichText( false );
168 textEdit->setPlainText( GuiUtils::contents( m_annot ) );
169 connect(textEdit,SIGNAL(textChanged()),
170 this,SLOT(slotsaveWindowText()));
172 QVBoxLayout * mainlay = new QVBoxLayout( this );
173 mainlay->setMargin( 2 );
174 mainlay->setSpacing( 0 );
175 m_title = new MovableTitle( this );
176 mainlay->addWidget( m_title );
177 mainlay->addWidget( textEdit );
178 QHBoxLayout * lowerlay = new QHBoxLayout();
179 mainlay->addLayout( lowerlay );
180 lowerlay->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Fixed ) );
181 QSizeGrip * sb = new QSizeGrip( this );
182 lowerlay->addWidget( sb );
184 m_title->setTitle( m_annot->window().summary() );
185 m_title->connectOptionButton( this, SLOT( slotOptionBtn() ) );
187 setGeometry(10,10,300,300 );
189 reloadInfo();
192 void AnnotWindow::reloadInfo()
194 const QColor newcolor = m_annot->style().color().isValid() ? m_annot->style().color() : Qt::yellow;
195 if ( newcolor != m_color )
197 m_color = newcolor;
198 setPalette( QPalette( m_color ) );
199 QPalette pl = textEdit->palette();
200 pl.setColor( QPalette::Base, m_color );
201 textEdit->setPalette( pl );
203 m_title->setAuthor( m_annot->author() );
204 m_title->setDate( m_annot->modificationDate() );
207 void AnnotWindow::slotOptionBtn()
209 //TODO: call context menu in pageview
210 //emit sig...
213 void AnnotWindow::slotsaveWindowText()
215 const QString newText = textEdit->toPlainText();
217 // 1. window text
218 if ( !m_annot->window().text().isEmpty() )
220 m_annot->window().setText( newText );
221 return;
223 // 2. if Text and InPlace, the inplace text
224 if ( m_annot->subType() == Okular::Annotation::AText )
226 Okular::TextAnnotation * txtann = static_cast< Okular::TextAnnotation * >( m_annot );
227 if ( txtann->textType() == Okular::TextAnnotation::InPlace )
229 txtann->setInplaceText( newText );
230 return;
234 // 3. contents
235 m_annot->setContents( newText );
238 #include "annotwindow.moc"