AlbumShaper  1.0a3
subalbumsWidget.cpp
Go to the documentation of this file.
1 //==============================================
2 // copyright : (C) 2003-2005 by Will Stokes
3 //==============================================
4 // This program is free software; you can redistribute it
5 // and/or modify it under the terms of the GNU General
6 // Public License as published by the Free Software
7 // Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //==============================================
10 
11 //Systemwide includes
12 #include <qwidget.h>
13 #include <qlayout.h>
14 #include <qlabel.h>
15 #include <qfont.h>
16 #include <qpixmap.h>
17 #include <qapplication.h>
18 #include <qtoolbutton.h>
19 #include <qtooltip.h>
20 //Added by qt3to4:
21 #include <Q3GridLayout>
22 #include <Q3Frame>
23 
24 //Projectwide includes
25 #include "subalbumsWidget.h"
26 #include "subalbumPreviewWidget.h"
27 #include "subalbumsIconView.h"
28 #include "layoutWidget.h"
29 #include "window.h"
30 #include "titleWidget.h"
31 #include "dialogs/questionDialog.h"
32 #include "../config.h"
33 #include "../backend/album.h"
34 #include "../backend/subalbum.h"
35 #include "../backend/tools/guiTools.h"
36 #include "../configuration/configuration.h"
37 
38 //==============================================
40  const char* name ) :
41  QWidget(parent,name)
42 {
43  //set layout pointer
44  layout = (LayoutWidget*)parent;
45 
46  //create "Collections:" header
47  QLabel* collectionsHeader = new QLabel( this );
48  collectionsHeader->setText( tr("Collections:") );
49  QFont labelFont = collectionsHeader->font();
50  labelFont.setWeight(QFont::Bold);
51  collectionsHeader->setFont( labelFont );
52  //--------------------------------------
53  //create collections list
54  collections = new SubalbumsIconView( this );
55 
56  //only one item can be selected at a time
57  collections->setSelectionMode( Q3IconView::Single ) ;
58 
59  //single column of items
60  collections->setGridX(1);
61 
62  //text is on right of icons
63  collections->setItemTextPos( Q3IconView::Right );
64 
65  //disable frame
66  collections->setFrameShape ( Q3Frame::NoFrame );
67 
68  collections->setMaxItemWidth(500);
69  collections->setPaletteBackgroundColor( QColor(193, 210, 238) );
70  collections->setDragAutoScroll(true);
71  collections->setAcceptDrops(true);
72  collections->setVScrollBarMode( Q3ScrollView::Auto );
73  collections->setHScrollBarMode( Q3ScrollView::Auto );
74  //--------------------------------------
75  //no selection by default
76  currentSelection = NULL;
77  //--------------------------------------
78  //connect drop event on iconview to reorder slot
79  connect( collections, SIGNAL(itemHasMoved()), SLOT(reorder()) );
80 
81  //handle selection attempts
82  connect( collections, SIGNAL(selectionChanged(Q3IconViewItem*)),
84  //--------------------------------------
85  //create create/delete buttons
86  QFont buttonFont( qApp->font() );
87  buttonFont.setBold(true);
88  buttonFont.setPointSize( 11 );
89 
90  createButton = new QToolButton( this );
91  createButton->setTextLabel(tr("Create"));
92  createButton->setIconSet( QPixmap(QString(IMAGE_PATH)+"buttonIcons/create.png") );
93  createButton->setTextPosition(QToolButton::BesideIcon);
94  createButton->setFont( buttonFont );
95  createButton->setUsesTextLabel( true );
96  createButton->setEnabled(true);
97  createButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
98  QToolTip::add( createButton, tr("Create a collection and append to subalbum list") );
99  connect( createButton, SIGNAL(clicked()), SLOT(createAction()) );
100 
101  deleteButton = new QToolButton( this );
102  deleteButton->setTextLabel(tr("Delete"));
103  deleteButton->setIconSet( QPixmap(QString(IMAGE_PATH)+"buttonIcons/delete.png") );
104  deleteButton->setTextPosition(QToolButton::BesideIcon);
105  deleteButton->setFont( buttonFont );
106  deleteButton->setUsesTextLabel( true );
107  deleteButton->setEnabled(false);
108  deleteButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
109  QToolTip::add( deleteButton, tr("Delete selected collection and all its contents") );
110  connect( deleteButton, SIGNAL(clicked()), SLOT(deleteAction()) );
111  //--------------------------------------
112  //place label, listbox, and buttons in grid
113  Q3GridLayout* grid = new Q3GridLayout( this, 3, 2, 0 );
114  grid->addMultiCellWidget( collectionsHeader, 0, 0, 0, 1, Qt::AlignHCenter );
115  grid->addMultiCellWidget( collections, 1, 1, 0, 1 );
116  grid->addWidget( createButton, 2, 0, Qt::AlignHCenter);
117  grid->addWidget( deleteButton, 2, 1, Qt::AlignHCenter);
118 
119  //allow collections listing to grow
120  grid->setRowStretch( 1, 1 );
121 
122  //set the background of the widget to be white
123  setPaletteBackgroundColor( QColor(193, 210, 238) );
124 }
125 //==============================================
127 {
128  //create new collection object
129  Album* albm = layout->getWindow()->getTitle()->getAlbum();
130  Subalbum* newCollection = new Subalbum( albm, albm->getNumSubalbums()+1 );
131  albm->appendSubalbum( newCollection );
132 
133  //create collection icon and auto select it
134  SubalbumPreviewWidget* newCollectionIcon = new SubalbumPreviewWidget( collections, newCollection );
135  newCollectionIcon->setDropEnabled(true);
136  selectCollection( newCollectionIcon );
137 
138  //update enabled state of delete collection button
139  deleteButton->setEnabled( collections->count() > 1 );
140 }
141 //==============================================
143 {
144  //if an item is selected it remove it
145  if(collections->currentItem() != NULL)
146  {
147  //if user has chosen to not receive destructive action warnings, or agrees to the action, then
148  //delete subalbum and refresh view
149  bool proceed = !((Window*)qApp->mainWidget())->getConfig()->getBool( "alerts", "showDestructiveAlerts" );
150  if(!proceed)
151  {
152  QuestionDialog sure( tr("Delete collection?"),
153  tr("Once deleted a collection and it's contents cannot be brought back unless a saved copy of the album exists."),
154  "alertIcons/warning.png",
155  this );
156  proceed = sure.exec();
157  }
158  if(proceed)
159  {
160  //get handle on currently selected collection
161  Q3IconViewItem* oldSelection = collections->currentItem();
162 
163  //get handle on the next automatically selected collection
164  //auto select the new collection. If there is no next
165  //collection, select the previous collection (again if present)
166  Q3IconViewItem* newSelection = oldSelection->nextItem();
167  if(newSelection == NULL) newSelection = oldSelection->prevItem();
168 
169  //auto select a remaining collection if one exists
170  //we select before removing and deleting the old collection so that
171  //the collection information above smoothly transitions
172  selectCollection( newSelection );
173 
174  //remove the collection from the album
175  Subalbum* s = ((SubalbumPreviewWidget*) oldSelection)->getSubalbum();
177 
178  //free the collection icon
179  delete oldSelection;
180  oldSelection = NULL;
181 
182  //rearrange the items in the grid, making
183  //sure new selection is visible
184  collections->arrangeItemsInGrid();
185  if(newSelection != NULL) collections->ensureItemVisible( newSelection );
186 
187  //update enabled state of delete collection button
188  deleteButton->setEnabled( collections->count() > 1 );
189 
190  //notifty title widget that the album's photo count has possible changed
192  }
193  }
194 }
195 //==============================================
197 {
198  if( currentSelection != NULL)
199  currentSelection->setText( ((SubalbumPreviewWidget*)currentSelection)->getSubalbum()->getName() );
200 }
201 //==============================================
203 {
204  if( currentSelection != NULL)
205  currentSelection->setPixmap( *val );
206 }
207 //==============================================
209 {
210  //so item has been moved, reorder linked list of items as necessary
211  collections->sort( true );
212  collections->arrangeItemsInGrid();
213 
214  //sync lists
215  Album* albm = layout->getWindow()->getTitle()->getAlbum();
216  albm->syncSubalbumList((SubalbumPreviewWidget*)collections->firstItem());
217 }
218 //==============================================
220 {
221  if(enable)
222  {
223  createButton->setEnabled( true );
224  deleteButton->setEnabled( buttonsState );
225  }
226  else
227  {
228  buttonsState = createButton->isEnabled();
229  createButton->setEnabled( false );
230  deleteButton->setEnabled( false );
231  }
232 }
233 //==============================================
236 //==============================================
238 {
239  return ((SubalbumPreviewWidget*) currentSelection )->getSubalbum();
240 }
241 //==============================================
243 {
244  //delete all previous entries
245  Q3IconViewItem* current = collections->firstItem();
246  while(current != NULL)
247  {
248  Q3IconViewItem* next = current->nextItem();
249  delete current;
250  current = next;
251  }
252 
253  //for some reason scrollbar does not disappear automatically.
254  //Calling clear fixes this.
255  collections->clear();
256 
257  //reset cached selection handle
258  currentSelection = NULL;
259 
260  //insert all collections
261  Subalbum* curCollection = layout->getWindow()->getTitle()->getAlbum()->getFirstSubalbum();
262  while( curCollection != NULL)
263  {
264  SubalbumPreviewWidget* item = new SubalbumPreviewWidget( collections, curCollection );
265  item->setDropEnabled(true);
266  curCollection = curCollection->getNext();
267  }
268 
269  //refresh iconview
270  collections->arrangeItemsInGrid();
271 
272  //auto select first item
274 }
275 //==============================================
277 {
278  //select collections only when program is not busy.
279  if( !layout->getWindow()->getTitle()->getBusy() )
280  selectCollection( item );
281 }
282 //==============================================
284 {
285  selectCollection( collections->firstItem() );
286 }
287 //==============================================
289 {
290  //no necessary action when selecting the currently selection collection
291  if(currentSelection == item) return;
292 
293  //select item
294  if( item != NULL ) collections->setSelected( item, true);
295 
296  //cachce selection
297  currentSelection = item;
298 
299  //emit signal that a different collection has been selected
300  if(currentSelection == NULL )
301  emit collectionSelected( NULL );
302  else
303  emit collectionSelected( ((SubalbumPreviewWidget*)currentSelection)->getSubalbum() );
304 }
305 //==============================================
306 /*
307  void SubalbumsWidget::setSelectedSubalbum( Subalbum* selection )
308 {
309  QIconViewItem* current = collections->firstItem();
310  while(current != NULL)
311  {
312  //found indicated collection
313  if( ((SubalbumPreviewWidget*)current)->getSubalbum() == selection )
314  {
315  current->setSelected( true );
316  break;
317  }
318 
319  //move to next item
320  current = current->nextItem();
321  }
322 }*/
323 //==============================================
324 
LayoutWidget * getParent()
Returns parent.
void selectCollection(Q3IconViewItem *item)
select specified collection
SubalbumsIconView * collections
list of subalbums
QToolButton * createButton
Create collection button.
LayoutWidget * layout
Pointer to layoutwidget this widget is in.
int getNumSubalbums()
Returns number of subalbums.
Definition: album.cpp:144
Extension of iconview, used to list all subalbums in album. Supports drag-n-drop within iconview such...
void appendSubalbum(Subalbum *val)
Appends subalbum to end of linked list.
Definition: album.cpp:234
QToolButton * deleteButton
Delete collection button.
Top level widget, encapsulates the title widget, the layout widget, and the toolbar widget...
Definition: window.h:39
void updateMenus(bool anySelected=false, bool anyRevertable=false)
update begin presentation menu entry - disabled when no photos in album
void refreshCollectionsList()
Refreshes list of collections, selecting first by default.
SubalbumsWidget(QWidget *parent=0, const char *name=0)
Creates layout.
Displays list of subalbums and a particular subalbum layout.
Definition: layoutWidget.h:39
QString IMAGE_PATH
Definition: config.cpp:18
void syncSubalbumList(SubalbumPreviewWidget *item)
Syncs subalbum ordering with front end gui ordering.
Definition: album.cpp:1369
bool getBusy()
is program busy?
Q3IconViewItem * getCurrentSelection()
Returns current selection.
A subalbum contains photos.
Definition: subalbum.h:48
void handleSelectionAttempt(Q3IconViewItem *item)
respond to user clicking collection icons
void removeSubalbum(Subalbum *val)
Removes a subalbum.
Definition: album.cpp:257
Displays subalbum icon and name.
void collectionSelected(Subalbum *)
Subalbum * getFirstSubalbum()
Returns a pointer to the first Subalbum.
Definition: album.cpp:135
void reorder()
relayout collectionicons after a create/delete refresh
void refreshSelectedCollectionName()
Subalbum * getSelectedSubalbum()
Returns the currently selected subalbum.
Window * getWindow()
Returns a pointer to the window.
void updateButtons(bool enable)
Activates/Deactives create/delete buttons.
void updatedSelectedCollectionImage(QPixmap *val)
An album contains Subalbums.
Definition: album.h:52
Subalbum * getNext()
Returns pointer to next subalbum.
Definition: subalbum.cpp:98
Q3IconViewItem * currentSelection
Album * getAlbum()
Returns a pointer to the album object.
void selectFirstCollection()
Select specified subalbum.
A configurable question dialog that returns true/false.
TitleWidget * getTitle()
returns a pointer to the title widget
Definition: window.cpp:188
void createAction()
create a new collection
bool buttonsState
Cached enabled/disabled state of buttons.
void deleteAction()
deletes the currently selected collection