1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/***************************************************************************
* Copyright (C) 2003 by *
* Unai Garro ([email protected]) *
* Cyril Bosselut ([email protected]) *
* Jason Kivlighn ([email protected]) *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "selectauthorsdialog.h"
#include <tqmessagebox.h>
#include <tqvbox.h>
#include <tdeconfig.h>
#include <kdialog.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdeglobal.h>
#include "backends/recipedb.h"
SelectAuthorsDialog::SelectAuthorsDialog( TQWidget *parent, const ElementList ¤tAuthors, RecipeDB *db )
: KDialogBase( parent, "SelectAuthorsDialog", true, i18n("Authors"),
KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok ),
database(db)
{
TQVBox *page = makeVBoxMainWidget();
//Design UI
// Combo to Pick authors
TQHBox *topBox = new TQHBox(page);
topBox->setSpacing(6);
authorsCombo = new KComboBox( true, topBox );
authorsCombo->setSizePolicy( TQSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding ) );
authorsCombo->completionObject() ->setCompletionMode( TDEGlobalSettings::CompletionPopupAuto );
authorsCombo->lineEdit() ->disconnect( authorsCombo ); //so hitting enter doesn't enter the item into the box
// Add/Remove buttons
il = new TDEIconLoader;
addAuthorButton = new TQPushButton( topBox );
TQPixmap pm = il->loadIcon( "go-down", TDEIcon::NoGroup, 16 );
addAuthorButton->setIconSet( pm );
removeAuthorButton = new TQPushButton( topBox );
pm = il->loadIcon( "go-up", TDEIcon::NoGroup, 16 );
removeAuthorButton->setIconSet( pm );
// Author List
authorListView = new TDEListView( page );
authorListView->setSizePolicy( TQSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding ) );
TDEConfig * config = TDEGlobal::config();
config->setGroup( "Advanced" );
bool show_id = config->readBoolEntry( "ShowID", false );
authorListView->addColumn( i18n( "Id" ), show_id ? -1 : 0 );
authorListView->addColumn( i18n( "Author" ) );
authorListView->setAllColumnsShowFocus( true );
// Load the list
loadAuthors( currentAuthors );
adjustSize();
resize(450, height());
// Connect signals & Slots
connect ( addAuthorButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( addAuthor() ) );
connect ( removeAuthorButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( removeAuthor() ) );
authorsCombo->setEditText(TQString::null);
authorsCombo->lineEdit()->setFocus();
}
SelectAuthorsDialog::~SelectAuthorsDialog()
{}
void SelectAuthorsDialog::getSelectedAuthors( ElementList *newAuthors )
{
for ( TQListViewItem * it = authorListView->firstChild();it; it = it->nextSibling() ) {
Element author;
author.id = it->text( 0 ).toInt();
author.name = it->text( 1 );
newAuthors->append( author );
}
}
void SelectAuthorsDialog::loadAuthors( const ElementList ¤tAuthors )
{
// Load the combo
reloadAuthorsCombo();
// Load the ListView with the authors of this recipe
authorListView->clear();
for ( ElementList::const_iterator author_it = currentAuthors.begin(); author_it != currentAuthors.end(); ++author_it ) {
( void ) new TQListViewItem( authorListView, TQString::number( ( *author_it ).id ), ( *author_it ).name );
}
}
void SelectAuthorsDialog::addAuthor( void )
{
//check bounds first
if ( authorsCombo->currentText().length() > uint(database->maxAuthorNameLength()) ) {
KMessageBox::error( this, TQString( i18n( "Author name cannot be longer than %1 characters." ) ).arg( database->maxAuthorNameLength() ) );
authorsCombo->lineEdit() ->selectAll();
return ;
}
if ( authorsCombo->lineEdit()->text().isEmpty() )
return;
if ( authorsCombo->contains( authorsCombo->currentText() ) )
authorsCombo->setCurrentItem( authorsCombo->currentText() );
createNewAuthorIfNecessary();
int currentItem = authorsCombo->currentItem();
Element currentElement = authorList.getElement( currentItem );
( void ) new TQListViewItem( authorListView, TQString::number( currentElement.id ), currentElement.name );
}
void SelectAuthorsDialog::removeAuthor( void )
{
// Find the selected item first
TQListViewItem * it;
it = authorListView->selectedItem();
if ( it ) { // Check if an author is selected first
delete it;
}
}
void SelectAuthorsDialog::createNewAuthorIfNecessary( void )
{
if ( !authorsCombo->contains( authorsCombo->currentText() ) &&
!( authorsCombo->currentText().stripWhiteSpace() ).isEmpty() ) // author is not in the list and is not empty
{ // Create new author
TQString newAuthorName = authorsCombo->currentText();
database->createNewAuthor( newAuthorName );
//List again the authors
reloadAuthorsCombo();
// Select the newly created author
authorsCombo->setCurrentItem( newAuthorName );
}
}
void SelectAuthorsDialog::reloadAuthorsCombo( void )
{
//Load the author list
database->loadAuthors( &authorList );
// Load combo with all the authors
authorsCombo->clear();
authorsCombo->completionObject() ->clear();
for ( ElementList::const_iterator author_it = authorList.begin(); author_it != authorList.end(); ++author_it ) {
authorsCombo->insertItem( ( *author_it ).name );
authorsCombo->completionObject() ->addItem( ( *author_it ).name );
}
}
#include "selectauthorsdialog.moc"
|