1 | /****************************************************************************
|
---|
2 | ** $Id: listviews.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
5 | **
|
---|
6 | ** This file is part of an example program for Qt. This example
|
---|
7 | ** program may be used, distributed and modified without limitation.
|
---|
8 | **
|
---|
9 | *****************************************************************************/
|
---|
10 |
|
---|
11 | #include "listviews.h"
|
---|
12 |
|
---|
13 | #include <qlabel.h>
|
---|
14 | #include <qpainter.h>
|
---|
15 | #include <qpalette.h>
|
---|
16 | #include <qobjectlist.h>
|
---|
17 | #include <qpopupmenu.h>
|
---|
18 | #include <qheader.h>
|
---|
19 | #include <qregexp.h>
|
---|
20 |
|
---|
21 | // -----------------------------------------------------------------
|
---|
22 |
|
---|
23 | MessageHeader::MessageHeader( const MessageHeader &mh )
|
---|
24 | {
|
---|
25 | msender = mh.msender;
|
---|
26 | msubject = mh.msubject;
|
---|
27 | mdatetime = mh.mdatetime;
|
---|
28 | }
|
---|
29 |
|
---|
30 | MessageHeader &MessageHeader::operator=( const MessageHeader &mh )
|
---|
31 | {
|
---|
32 | msender = mh.msender;
|
---|
33 | msubject = mh.msubject;
|
---|
34 | mdatetime = mh.mdatetime;
|
---|
35 |
|
---|
36 | return *this;
|
---|
37 | }
|
---|
38 |
|
---|
39 | // -----------------------------------------------------------------
|
---|
40 |
|
---|
41 | Folder::Folder( Folder *parent, const QString &name )
|
---|
42 | : QObject( parent, name ), fName( name )
|
---|
43 | {
|
---|
44 | lstMessages.setAutoDelete( TRUE );
|
---|
45 | }
|
---|
46 |
|
---|
47 | // -----------------------------------------------------------------
|
---|
48 |
|
---|
49 | FolderListItem::FolderListItem( QListView *parent, Folder *f )
|
---|
50 | : QListViewItem( parent )
|
---|
51 | {
|
---|
52 | myFolder = f;
|
---|
53 | setText( 0, f->folderName() );
|
---|
54 |
|
---|
55 | if ( myFolder->children() )
|
---|
56 | insertSubFolders( myFolder->children() );
|
---|
57 | }
|
---|
58 |
|
---|
59 | FolderListItem::FolderListItem( FolderListItem *parent, Folder *f )
|
---|
60 | : QListViewItem( parent )
|
---|
61 | {
|
---|
62 | myFolder = f;
|
---|
63 |
|
---|
64 | setText( 0, f->folderName() );
|
---|
65 |
|
---|
66 | if ( myFolder->children() )
|
---|
67 | insertSubFolders( myFolder->children() );
|
---|
68 | }
|
---|
69 |
|
---|
70 | void FolderListItem::insertSubFolders( const QObjectList *lst )
|
---|
71 | {
|
---|
72 | Folder *f;
|
---|
73 | for ( f = ( Folder* )( ( QObjectList* )lst )->first(); f; f = ( Folder* )( ( QObjectList* )lst )->next() )
|
---|
74 | (void)new FolderListItem( this, f );
|
---|
75 | }
|
---|
76 |
|
---|
77 | // -----------------------------------------------------------------
|
---|
78 |
|
---|
79 | MessageListItem::MessageListItem( QListView *parent, Message *m )
|
---|
80 | : QListViewItem( parent )
|
---|
81 | {
|
---|
82 | myMessage = m;
|
---|
83 | setText( 0, myMessage->header().sender() );
|
---|
84 | setText( 1, myMessage->header().subject() );
|
---|
85 | setText( 2, myMessage->header().datetime().toString() );
|
---|
86 | }
|
---|
87 |
|
---|
88 | void MessageListItem::paintCell( QPainter *p, const QColorGroup &cg,
|
---|
89 | int column, int width, int alignment )
|
---|
90 | {
|
---|
91 | QColorGroup _cg( cg );
|
---|
92 | QColor c = _cg.text();
|
---|
93 |
|
---|
94 | if ( myMessage->state() == Message::Unread )
|
---|
95 | _cg.setColor( QColorGroup::Text, Qt::red );
|
---|
96 |
|
---|
97 | QListViewItem::paintCell( p, _cg, column, width, alignment );
|
---|
98 |
|
---|
99 | _cg.setColor( QColorGroup::Text, c );
|
---|
100 | }
|
---|
101 |
|
---|
102 | // -----------------------------------------------------------------
|
---|
103 |
|
---|
104 | ListViews::ListViews( QWidget *parent, const char *name )
|
---|
105 | : QSplitter( Qt::Horizontal, parent, name )
|
---|
106 | {
|
---|
107 | lstFolders.setAutoDelete( TRUE );
|
---|
108 |
|
---|
109 | folders = new QListView( this );
|
---|
110 | folders->header()->setClickEnabled( FALSE );
|
---|
111 | folders->addColumn( "Folder" );
|
---|
112 |
|
---|
113 | initFolders();
|
---|
114 | setupFolders();
|
---|
115 |
|
---|
116 | folders->setRootIsDecorated( TRUE );
|
---|
117 | setResizeMode( folders, QSplitter::KeepSize );
|
---|
118 |
|
---|
119 | QSplitter *vsplitter = new QSplitter( Qt::Vertical, this );
|
---|
120 |
|
---|
121 | messages = new QListView( vsplitter );
|
---|
122 | messages->addColumn( "Sender" );
|
---|
123 | messages->addColumn( "Subject" );
|
---|
124 | messages->addColumn( "Date" );
|
---|
125 | messages->setColumnAlignment( 1, Qt::AlignRight );
|
---|
126 | messages->setAllColumnsShowFocus( TRUE );
|
---|
127 | messages->setShowSortIndicator( TRUE );
|
---|
128 | menu = new QPopupMenu( messages );
|
---|
129 | for( int i = 1; i <= 10; i++ )
|
---|
130 | menu->insertItem( QString( "Context Item %1" ).arg( i ) );
|
---|
131 | connect(messages, SIGNAL( contextMenuRequested( QListViewItem *, const QPoint& , int ) ),
|
---|
132 | this, SLOT( slotRMB( QListViewItem *, const QPoint &, int ) ) );
|
---|
133 | vsplitter->setResizeMode( messages, QSplitter::KeepSize );
|
---|
134 |
|
---|
135 | message = new QLabel( vsplitter );
|
---|
136 | message->setAlignment( Qt::AlignTop );
|
---|
137 | message->setBackgroundMode( PaletteBase );
|
---|
138 |
|
---|
139 | connect( folders, SIGNAL( selectionChanged( QListViewItem* ) ),
|
---|
140 | this, SLOT( slotFolderChanged( QListViewItem* ) ) );
|
---|
141 | connect( messages, SIGNAL( selectionChanged() ),
|
---|
142 | this, SLOT( slotMessageChanged() ) );
|
---|
143 | connect( messages, SIGNAL( currentChanged( QListViewItem * ) ),
|
---|
144 | this, SLOT( slotMessageChanged() ) );
|
---|
145 |
|
---|
146 | messages->setSelectionMode( QListView::Extended );
|
---|
147 | // some preparations
|
---|
148 | folders->firstChild()->setOpen( TRUE );
|
---|
149 | folders->firstChild()->firstChild()->setOpen( TRUE );
|
---|
150 | folders->setCurrentItem( folders->firstChild()->firstChild()->firstChild() );
|
---|
151 | folders->setSelected( folders->firstChild()->firstChild()->firstChild(), TRUE );
|
---|
152 |
|
---|
153 | messages->setSelected( messages->firstChild(), TRUE );
|
---|
154 | messages->setCurrentItem( messages->firstChild() );
|
---|
155 | message->setMargin( 5 );
|
---|
156 |
|
---|
157 | QValueList<int> lst;
|
---|
158 | lst.append( 170 );
|
---|
159 | setSizes( lst );
|
---|
160 | }
|
---|
161 |
|
---|
162 | void ListViews::initFolders()
|
---|
163 | {
|
---|
164 | unsigned int mcount = 1;
|
---|
165 |
|
---|
166 | for ( unsigned int i = 1; i < 20; i++ ) {
|
---|
167 | QString str;
|
---|
168 | str = QString( "Folder %1" ).arg( i );
|
---|
169 | Folder *f = new Folder( 0, str );
|
---|
170 | for ( unsigned int j = 1; j < 5; j++ ) {
|
---|
171 | QString str2;
|
---|
172 | str2 = QString( "Sub Folder %1" ).arg( j );
|
---|
173 | Folder *f2 = new Folder( f, str2 );
|
---|
174 | for ( unsigned int k = 1; k < 3; k++ ) {
|
---|
175 | QString str3;
|
---|
176 | str3 = QString( "Sub Sub Folder %1" ).arg( k );
|
---|
177 | Folder *f3 = new Folder( f2, str3 );
|
---|
178 | initFolder( f3, mcount );
|
---|
179 | }
|
---|
180 | }
|
---|
181 | lstFolders.append( f );
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | void ListViews::initFolder( Folder *folder, unsigned int &count )
|
---|
186 | {
|
---|
187 | for ( unsigned int i = 0; i < 15; i++, count++ ) {
|
---|
188 | QString str;
|
---|
189 | str = QString( "Message %1 " ).arg( count );
|
---|
190 | QDateTime dt = QDateTime::currentDateTime();
|
---|
191 | dt = dt.addSecs( 60 * count );
|
---|
192 | MessageHeader mh( "Trolltech <info@trolltech.com> ", str, dt );
|
---|
193 |
|
---|
194 | QString body;
|
---|
195 | body = QString( "This is the message number %1 of this application, \n"
|
---|
196 | "which shows how to use QListViews, QListViewItems, \n"
|
---|
197 | "QSplitters and so on. The code should show how easy\n"
|
---|
198 | "this can be done in Qt." ).arg( count );
|
---|
199 | Message *msg = new Message( mh, body );
|
---|
200 | folder->addMessage( msg );
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | void ListViews::setupFolders()
|
---|
205 | {
|
---|
206 | folders->clear();
|
---|
207 |
|
---|
208 | for ( Folder* f = lstFolders.first(); f; f = lstFolders.next() )
|
---|
209 | (void)new FolderListItem( folders, f );
|
---|
210 | }
|
---|
211 |
|
---|
212 | void ListViews::slotRMB( QListViewItem* Item, const QPoint & point, int )
|
---|
213 | {
|
---|
214 | if( Item )
|
---|
215 | menu->popup( point );
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | void ListViews::slotFolderChanged( QListViewItem *i )
|
---|
220 | {
|
---|
221 | if ( !i )
|
---|
222 | return;
|
---|
223 | messages->clear();
|
---|
224 | message->setText( "" );
|
---|
225 |
|
---|
226 | FolderListItem *item = ( FolderListItem* )i;
|
---|
227 |
|
---|
228 | for ( Message* msg = item->folder()->firstMessage(); msg;
|
---|
229 | msg = item->folder()->nextMessage() )
|
---|
230 | (void)new MessageListItem( messages, msg );
|
---|
231 | }
|
---|
232 |
|
---|
233 | void ListViews::slotMessageChanged()
|
---|
234 | {
|
---|
235 | QListViewItem *i = messages->currentItem();
|
---|
236 | if ( !i )
|
---|
237 | return;
|
---|
238 |
|
---|
239 | if ( !i->isSelected() ) {
|
---|
240 | message->setText( "" );
|
---|
241 | return;
|
---|
242 | }
|
---|
243 |
|
---|
244 | MessageListItem *item = ( MessageListItem* )i;
|
---|
245 | Message *msg = item->message();
|
---|
246 |
|
---|
247 | QString text;
|
---|
248 | QString tmp = msg->header().sender();
|
---|
249 | tmp = tmp.replace( "<", "<" );
|
---|
250 | tmp = tmp.replace( ">", ">" );
|
---|
251 | text = QString( "<b><i>From:</i></b> <a href=\"mailto:info@trolltech.com\">%1</a><br>"
|
---|
252 | "<b><i>Subject:</i></b> <big><big><b>%2</b></big></big><br>"
|
---|
253 | "<b><i>Date:</i></b> %3<br><br>"
|
---|
254 | "%4" ).
|
---|
255 | arg( tmp ).arg( msg->header().subject() ).
|
---|
256 | arg( msg->header().datetime().toString() ).arg( msg->body() );
|
---|
257 |
|
---|
258 | message->setText( text );
|
---|
259 |
|
---|
260 | msg->setState( Message::Read );
|
---|
261 | }
|
---|