OSDN Git Service

add close button.
[kita/kita.git] / kita / src / threadlistview.cpp
1 /***************************************************************************
2 *   Copyright (C) 2003 by Hideki Ikemoto                                  *
3 *   ikemo@wakaba.jp                                                       *
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 ***************************************************************************/
10
11 #include "threadlistview.h"
12
13 #include <kiconloader.h>
14 #include <klistview.h>
15 #include <klocale.h>
16 #include <kurl.h>
17
18 #include <qtoolbutton.h>
19 #include <qcombobox.h>
20 #include <qregexp.h>
21 #include <qheader.h>
22
23 #include "libkita/thread.h"
24 #include "libkita/kitaconfig.h"
25 #include "libkita/signalcollection.h"
26
27 using namespace Kita;
28
29 ThreadListView::ThreadListView( QWidget* parent, const char* name )
30         : ThreadListViewBase( parent, name )
31 {
32     SearchButton->setPixmap( SmallIcon( "find" ) );
33     HideButton->setPixmap( SmallIcon( "filter" ) );
34     ReloadButton->setPixmap( SmallIcon( "reload" ) );
35     closeButton->setPixmap( SmallIcon( "fileclose" ) );
36
37     subjectList->addColumn( "" );
38     subjectList->addColumn( i18n( "No." ) );
39     subjectList->addColumn( "" );
40     subjectList->addColumn( i18n( "Title" ) );
41     subjectList->addColumn( i18n( "ResNum" ) );
42     subjectList->addColumn( i18n( "ReadNum" ) );
43     subjectList->addColumn( i18n( "Unread" ) );
44     subjectList->addColumn( i18n( "Since" ) );
45     subjectList->addColumn( i18n( "Thread's speed" ) );
46
47     QHeader *h = subjectList->header();
48     h->setResizeEnabled( FALSE, Col_Mark );
49     h->setResizeEnabled( FALSE, Col_Icon );
50     h->setStretchEnabled( TRUE, Col_Subject );
51
52     //subjectList->setColumnWidth( Col_Subject, 400 );
53     subjectList->setColumnWidthMode( Col_Subject, QListView::Manual );
54
55     connect( SearchButton, SIGNAL( clicked() ),
56              SLOT( slotSearchButton() ) );
57     connect( SearchCombo, SIGNAL( activated( int ) ),
58              SLOT( slotSearchButton() ) );
59     connect( HideButton, SIGNAL( toggled( bool ) ),
60              SLOT( slotHideButton( bool ) ) );
61     connect( subjectList, SIGNAL( mouseButtonClicked( int, QListViewItem*, const QPoint&, int ) ),
62              SLOT( slotMouseButtonClicked( int, QListViewItem* ) ) );
63     connect( this, SIGNAL( bookmarked( const QString&, bool ) ),
64              Kita::SignalCollection::getInstance(), SIGNAL( bookmarked( const QString&, bool ) ) );
65     connect( this, SIGNAL( openURLRequestExt(
66                                const KURL&, const KParts::URLArgs&, QString, int, int,
67                                const KURL&, const KURL&, const QString&, const QString& ) ),
68              Kita::SignalCollection::getInstance(), SIGNAL( openURLRequestExt(
69                          const KURL& , const KParts::URLArgs&, QString, int, int,
70                          const KURL&, const KURL&, const QString&, const QString& ) ) );
71 }
72
73 ThreadListView::~ThreadListView()
74 {}
75
76 void ThreadListView::slotSearchButton()
77 {
78     insertSearchCombo();
79     QStringList list = parseSearchQuery( SearchCombo->currentText() );
80     searchNext( list );
81 }
82
83 void ThreadListView::insertSearchCombo()
84 {
85     for ( int count = 0; count < SearchCombo->count(); ++count ) {
86         if ( SearchCombo->text( count ) == SearchCombo->currentText() ) {
87             return ;
88         }
89     }
90     SearchCombo->insertItem( SearchCombo->currentText() );
91 }
92
93 QStringList ThreadListView::parseSearchQuery( const QString &input )
94 {
95     QStringList tmp = QStringList::split( ' ', input );
96     QStringList ret_list;
97     QRegExp truncSpace( "\\s*$" );
98     QStringList::iterator it = tmp.begin();
99
100     for ( ; it != tmp.end(); ++it ) {
101         ret_list += ( *it ).replace( truncSpace, "" );
102     }
103     return ret_list;
104 }
105
106 void ThreadListView::searchNext( const QStringList &query )
107 {
108     if ( query.isEmpty() ) return ;
109
110     if ( query != m_prevquery ) {
111         searchAll( query );
112         slotHideButton( HideButton->isOn() );
113         m_nextHitIndex = 0; //A next jump-search target reset to '0'.
114         return ;
115     }
116
117     if ( m_nextHitIndex >= m_hitList.size() ) {
118         return ;
119     }
120
121     KListViewItem* item = m_hitList[ m_nextHitIndex ];
122     subjectList->ensureItemVisible( item );
123     subjectList->setSelected( item, true );
124     m_nextHitIndex++;
125     if ( m_nextHitIndex >= m_hitList.size() ) m_nextHitIndex = 0;
126 }
127
128 void ThreadListView::searchAll( const QStringList &query )
129 {
130     m_hitList.clear();
131     m_prevquery = query;
132
133     QListViewItemIterator listIt( subjectList );
134     while ( listIt.current() != 0 ) {
135         KListViewItem * item = static_cast<KListViewItem*>( listIt.current() );
136         item->setPixmap( Col_Icon, 0 );
137
138         QStringList::const_iterator queryIt = query.begin();
139         for ( ; queryIt != query.end(); ++queryIt ) {
140             if ( item->text( Col_Subject ).contains( *queryIt, false ) ) {
141                 item->setPixmap( Col_Icon, SmallIcon( "find" ) );
142                 m_hitList.append( item );
143                 break;
144             }
145         }
146         ++listIt;
147     }
148 }
149
150 void ThreadListView::slotHideButton( bool on )
151 {
152     if ( m_hitList.empty() ) return ;
153
154     QListViewItemIterator listIt( subjectList );
155     while ( listIt.current() != 0 ) {
156         KListViewItem * item = static_cast<KListViewItem *>( listIt.current() );
157         if ( on && ! item->pixmap( Col_Icon ) ) {
158             item->setVisible( false );
159         } else {
160             item->setVisible( true );
161         }
162         ++listIt;
163     }
164 }
165
166 void ThreadListView::slotMouseButtonClicked( int button, QListViewItem* item )
167 {
168     if ( ! item ) return ;
169
170     KURL datURL = item->text( Col_DatURL );
171
172     switch ( button ) {
173     case MidButton:
174         emit openURLRequestExt( datURL.prettyURL(), KParts::URLArgs(), "kita_open_2chthread", 1 );
175         break;
176     case LeftButton:
177         if ( KitaConfig::alwaysUseTab() ) {
178             emit openURLRequestExt( datURL.prettyURL(), KParts::URLArgs(), "kita_open_2chthread", 1 );
179         } else {
180             emit openURLRequestExt( datURL.prettyURL(), KParts::URLArgs(), "kita_open_2chthread", 0 );
181         }
182         break;
183     }
184 }
185
186 int ThreadListViewItem::compare( QListViewItem* i, int col, bool ascending ) const
187 {
188     switch ( col ) {
189     case Col_ResNum:
190     case Col_Read:
191     case Col_Unread:
192         return i->key( col, ascending ).toInt() - key( col, ascending ).toInt();
193     case Col_ID:
194         return key( col, ascending ).toInt() - i->key( col, ascending ).toInt();
195     case Col_Mark:
196         return QString::localeAwareCompare( i->key( Col_MarkOrder, ascending ), key( Col_MarkOrder, ascending ) );
197     case Col_Since:
198         return QString::localeAwareCompare( i->key( col, ascending ), key( col, ascending ) );
199     case Col_Speed:
200         return static_cast<int>( i->key( col, ascending ).toDouble() * 1000 - key( col, ascending ).toDouble() * 1000 );
201     default:
202         return QString::localeAwareCompare( key( col, ascending ), i->key( col, ascending ) );
203     }
204 }
205
206 #include "threadlistview.moc"