1 /***************************************************************************
2 * Copyright (C) 2006 by Chu Xiaodong <xiaodongchu@gmail.com> *
3 * Copyright (C) 2006 by Pino Toscano <pino@kde.org> *
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"
14 #include <qapplication.h>
17 #include <qfontinfo.h>
18 #include <qfontmetrics.h>
23 #include <qpushbutton.h>
24 #include <qsizegrip.h>
26 #include <qtoolbutton.h>
29 #include <ktextedit.h>
33 #include "core/annotations.h"
40 CloseButton( QWidget
* parent
= 0 )
41 : QPushButton( parent
)
43 setSizePolicy( QSizePolicy::Fixed
, QSizePolicy::Fixed
);
44 QSize size
= QSize( 14, 14 ).expandedTo( QApplication::globalStrut() );
46 setIcon( style()->standardIcon( QStyle::SP_DockWidgetCloseButton
) );
48 setToolTip( i18n( "Close this note" ) );
57 MovableTitle( QWidget
* parent
)
60 QVBoxLayout
* mainlay
= new QVBoxLayout( this );
61 mainlay
->setMargin( 0 );
62 mainlay
->setSpacing( 0 );
64 QHBoxLayout
* buttonlay
= new QHBoxLayout();
65 mainlay
->addLayout( buttonlay
);
66 titleLabel
= new QLabel( this );
67 QFont f
= titleLabel
->font();
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
);
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
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
)
110 QMouseEvent
* me
= 0;
113 case QEvent::MouseButtonPress
:
114 me
= (QMouseEvent
*)e
;
115 mousePressPos
= me
->pos();
117 case QEvent::MouseButtonRelease
:
118 mousePressPos
= QPoint();
120 case QEvent::MouseMove
:
121 me
= (QMouseEvent
*)e
;
122 parentWidget()->move( me
->pos() - mousePressPos
+ parentWidget()->pos() );
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
);
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 );
192 void AnnotWindow::reloadInfo()
194 const QColor newcolor
= m_annot
->style().color().isValid() ? m_annot
->style().color() : Qt::yellow
;
195 if ( newcolor
!= m_color
)
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
213 void AnnotWindow::slotsaveWindowText()
215 const QString newText
= textEdit
->toPlainText();
218 if ( !m_annot
->window().text().isEmpty() )
220 m_annot
->window().setText( newText
);
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
);
235 m_annot
->setContents( newText
);
238 #include "annotwindow.moc"