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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QIcon>
#include <QSize>
#include <QDebug>
#include "UBLibWidget.h"
#include "UBLibActionBar.h"
#include "core/UBApplication.h"
#include "core/memcheck.h"
/**
* \brief Constructor
* @param parent as the parent widget
* @param name as the object name
*/
UBLibActionBar::UBLibActionBar(QWidget *parent, const char *name):QWidget(parent)
, mCrntButtonSet(eButtonSet_Default)
, mPreviousButtonSet(eButtonSet_Default)
, mButtonGroup(NULL)
, mSearchBar(NULL)
, mLayout(NULL)
, mpFavoriteAction(NULL)
, mpSocialAction(NULL)
, mpDeleteAction(NULL)
, mpSearchAction(NULL)
, mpCloseAction(NULL)
, mpRemoveFavorite(NULL)
, mpNewFolderAction(NULL)
, mpFavoriteBtn(NULL)
, mpSocialBtn(NULL)
, mpDeleteBtn(NULL)
, mpSearchBtn(NULL)
, mpCloseBtn(NULL)
, mpRemoveFavoriteBtn(NULL)
, mpNewFolderBtn(NULL)
, bFavorite(false)
, bIsInTrash(false)
{
setObjectName(name);
setStyleSheet(QString("background: #EEEEEE; border-radius : 10px; border : 2px solid #999999;"));
setAcceptDrops(true);
mButtonGroup = new QButtonGroup(this);
mSearchBar = new QLineEdit(this);
mSearchBar->setStyleSheet(QString("background-color:white; border-radius : 10px; padding : 2px;"));
connect(mSearchBar, SIGNAL(returnPressed()), this, SLOT(onActionSearch()));
mLayout = new QHBoxLayout();
setLayout(mLayout);
setMaximumHeight(ACTIONBAR_HEIGHT);
// Create the actions
mpFavoriteAction = new QAction(QIcon(":/images/libpalette/miniFavorite.png"), tr("Add to favorites"), this);
mpSocialAction = new QAction(QIcon(":/images/libpalette/social.png"), tr("Share"), this);
mpSearchAction = new QAction(QIcon(":/images/libpalette/miniSearch.png"), tr("Search"), this);
mpDeleteAction = new QAction(QIcon(":/images/libpalette/miniTrash.png"), tr("Delete"), this);
mpCloseAction = new QAction(QIcon(":/images/close.svg"), tr("Back to folder"), this);
mpRemoveFavorite = new QAction(QIcon(":/images/libpalette/trash_favorite.svg"), tr("Remove from favorites"), this);
mpNewFolderAction = new QAction(QIcon(":/images/libpalette/miniNewFolder.png"), tr("Create new folder"), this);
// Create the buttons
mpFavoriteBtn = new UBActionButton(this, mpFavoriteAction);
mpSocialBtn = new UBActionButton(this, mpSocialAction);
mpSearchBtn = new UBActionButton(this, mpSearchAction);
mpDeleteBtn = new UBActionButton(this, mpDeleteAction);
mpCloseBtn = new UBActionButton(this, mpCloseAction);
mpRemoveFavoriteBtn = new UBActionButton(this, mpRemoveFavorite);
mpNewFolderBtn = new UBActionButton(this, mpNewFolderAction);
// Initialize the buttons
mpSearchBtn->setEnabled(false);
mpNewFolderBtn->setEnabled(false);
// Add the buttons to the button group
mButtonGroup->addButton(mpFavoriteBtn);
mButtonGroup->addButton(mpSocialBtn);
mButtonGroup->addButton(mpSearchBtn);
mButtonGroup->addButton(mpDeleteBtn);
mButtonGroup->addButton(mpCloseBtn);
mButtonGroup->addButton(mpRemoveFavoriteBtn);
mButtonGroup->addButton(mpNewFolderBtn);
// Connect signals & slots
connect(mpFavoriteAction,SIGNAL(triggered()), this, SLOT(onActionFavorite()));
connect(mpSocialAction,SIGNAL(triggered()), this, SLOT(onActionSocial()));
connect(mpSearchAction,SIGNAL(triggered()), this, SLOT(onActionSearch()));
connect(mpDeleteAction,SIGNAL(triggered()), this, SLOT(onActionTrash()));
connect(mpCloseAction, SIGNAL(triggered()), this, SLOT(onActionClose()));
connect(mpRemoveFavorite, SIGNAL(triggered()), this, SLOT(onActionRemoveFavorite()));
connect(mSearchBar, SIGNAL(textChanged(QString)), this, SLOT(onSearchTextChanged(QString)));
connect(mpNewFolderAction, SIGNAL(triggered()), this, SLOT(onActionNewFolder()));
// Build the default toolbar
mLayout->addWidget(mpFavoriteBtn);
mLayout->addWidget(mpSocialBtn);
mLayout->addWidget(mpNewFolderBtn);
mLayout->addWidget(mSearchBar);
mLayout->addWidget(mpSearchBtn);
mLayout->addWidget(mpDeleteBtn);
mLayout->addWidget(mpCloseBtn);
mLayout->addWidget(mpRemoveFavoriteBtn);
setButtons(eButtonSet_Default);
}
/**
* \brief Destructor
*/
UBLibActionBar::~UBLibActionBar()
{
if(NULL != mpNewFolderAction)
{
delete mpNewFolderAction;
mpNewFolderAction = NULL;
}
if(NULL != mpNewFolderBtn)
{
delete mpNewFolderBtn;
mpNewFolderBtn = NULL;
}
if(NULL != mpRemoveFavorite)
{
delete mpRemoveFavorite;
mpRemoveFavorite = NULL;
}
if(NULL != mpRemoveFavoriteBtn)
{
delete mpRemoveFavoriteBtn;
mpRemoveFavoriteBtn = NULL;
}
if(NULL != mpCloseAction)
{
delete mpCloseAction;
mpCloseAction = NULL;
}
if(NULL != mpDeleteAction)
{
delete mpDeleteAction;
mpDeleteAction = NULL;
}
if(NULL != mpFavoriteAction)
{
delete mpFavoriteAction;
mpFavoriteAction = NULL;
}
if(NULL != mpSearchAction)
{
delete mpSearchAction;
mpSearchAction = NULL;
}
if(NULL != mpSocialAction)
{
delete mpSocialAction;
mpSocialAction = NULL;
}
if(NULL != mpCloseBtn)
{
delete mpCloseBtn;
mpCloseBtn = NULL;
}
if(NULL != mpDeleteBtn)
{
delete mpDeleteBtn;
mpDeleteBtn = NULL;
}
if(NULL != mpFavoriteBtn)
{
delete mpFavoriteBtn;
mpFavoriteBtn = NULL;
}
if(NULL != mpSearchBtn)
{
delete mpSearchBtn;
mpSearchBtn = NULL;
}
if(NULL != mpSocialBtn)
{
delete mpSocialBtn;
mpSocialBtn = NULL;
}
if(NULL != mButtonGroup)
{
delete mButtonGroup;
mButtonGroup = NULL;
}
if(NULL != mSearchBar)
{
delete mSearchBar;
mSearchBar = NULL;
}
if(NULL != mLayout)
{
delete mLayout;
mLayout = NULL;
}
}
/**
* \brief Set the buttons of the action bar
* @param setID as the button set
*/
void UBLibActionBar::setButtons(eButtonSet setID)
{
mPreviousButtonSet = mCrntButtonSet;
mCrntButtonSet = setID;
switch(setID)
{
case eButtonSet_Default:
mpFavoriteBtn->show();
mpSocialBtn->hide();
mSearchBar->show();
mpSearchBtn->show();
mpDeleteBtn->show();
mpCloseBtn->hide();
mpRemoveFavoriteBtn->hide();
mpNewFolderBtn->show();
break;
case eButtonSet_Properties:
mpFavoriteBtn->show();
mpSocialBtn->hide();
mSearchBar->show();
mpSearchBtn->show();
mpDeleteBtn->hide();
mpCloseBtn->hide();
mpRemoveFavoriteBtn->hide();
mpNewFolderBtn->hide();
break;
case eButtonSet_Favorite:
mpFavoriteBtn->hide();
mpSocialBtn->hide();
mSearchBar->show();
mpSearchBtn->show();
mpDeleteBtn->hide();
mpCloseBtn->hide();
mpRemoveFavoriteBtn->show();
mpNewFolderBtn->hide();
break;
default:
break;
}
}
/**
* \brief (un)set the selected element to favorite
*/
void UBLibActionBar::onActionFavorite()
{
mpFavoriteBtn->setIcon(QIcon(":/images/libpalette/miniFavorite.png"));
libraryController()->addToFavorite(mSelectedElements);
}
/**
* \brief Handle the mouse enter event
* @param event as the event
*/
void UBLibActionBar::enterEvent(QEvent *event)
{
Q_UNUSED(event);
setCursor(Qt::ArrowCursor);
}
/**
* \brief Handle the mouse leave event
* @param event as the event
*/
void UBLibActionBar::leaveEvent(QEvent *event)
{
Q_UNUSED(event);
unsetCursor();
}
/**
* \brief Perform the search
*/
void UBLibActionBar::onActionSearch()
{
emit searchElement(mSearchBar->text());
}
/**
* \brief Trigger the social action
*/
void UBLibActionBar::onActionSocial()
{
// To be implemented
}
/**
* \brief Handles the close action
*/
void UBLibActionBar::onActionClose()
{
emit showFolderContent();
}
/**
* \brief Delete the selected element
*/
void UBLibActionBar::onActionTrash()
{
if(!bIsInTrash)
{
libraryController()->trashElements(mSelectedElements);
}
else
{
libraryController()->emptyElementsOnTrash(mSelectedElements);
}
emit deleteDone();
}
/**
* \brief Remove the selected favorite(s)
*/
void UBLibActionBar::onActionRemoveFavorite()
{
libraryController()->removeFromFavorite(mSelectedElements);
emit deleteDone();
}
/**
* \brief Handles the selection change event
* @param itemList as the list of selected items
* @param isInTrash indicates if the current folder is the trash
*/
void UBLibActionBar::onSelectionChanged(QList<UBLibElement *> itemList, bool isInTrash)
{
bIsInTrash = isInTrash;
mSelectedElements = itemList;
bool bEnable = (itemList.count() != 0) ? true : false;
if(mCrntButtonSet == eButtonSet_Favorite)
{
mpRemoveFavoriteBtn->setEnabled(bEnable);
return;
}
mpFavoriteAction->setEnabled(bEnable);
mpSocialAction->setEnabled(bEnable);
mpDeleteAction->setEnabled(bEnable && libraryController()->canItemsOnElementBeDeleted(itemList.at(0)));
}
/**
* \brief Get the library controller
* @return a pointer on the library controller
*/
UBLibraryController* UBLibActionBar::libraryController()
{
UBLibWidget* libWidget = dynamic_cast<UBLibWidget*>(parentWidget());
return libWidget->libNavigator()->libraryWidget()->libraryController();
}
/**
* \brief Show the actions related to the Favorites folder
*/
void UBLibActionBar::onFavoritesEntered(bool bFav)
{
setButtons(bFav ? eButtonSet_Favorite : eButtonSet_Default);
}
/**
* \brief Handles the drag enter event
* @param event as the drag enter event
*/
void UBLibActionBar::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
/**
* \brief Handles the drag move event
* @param event as the drag move event
*/
void UBLibActionBar::dragMoveEvent(QDragMoveEvent *event)
{
event->acceptProposedAction();
}
/**
* \brief Handles the drop event
* @param event as the given drop event
*/
void UBLibActionBar::dropEvent(QDropEvent *event)
{
const QPoint droppedPoint = event->pos();
QWidget* pTargetW = widgetAtPos(droppedPoint);
if(NULL != pTargetW)
{
if(mpFavoriteBtn == pTargetW)
{
onActionFavorite();
}
else if(mpRemoveFavoriteBtn == pTargetW)
{
onActionRemoveFavorite();
}
else if(mpDeleteBtn == pTargetW)
{
if(mpDeleteBtn->isEnabled())
{
onActionTrash();
}
}
else if(mpSocialBtn == pTargetW)
{
onActionSocial();
}
}
event->acceptProposedAction();
}
/**
* \brief Get the widget at the given position
* @param p as the given position
* @return a pointer on the related QWidget
*/
QWidget* UBLibActionBar::widgetAtPos(const QPoint p)
{
Q_ASSERT(mpDeleteBtn != NULL);
Q_ASSERT(mpFavoriteBtn != NULL);
Q_ASSERT(mpRemoveFavoriteBtn != NULL);
Q_ASSERT(mpSocialBtn != NULL);
QList<UBActionButton*> qlBttns;
qlBttns << mpFavoriteBtn;
qlBttns << mpDeleteBtn;
qlBttns << mpRemoveFavoriteBtn;
qlBttns << mpSocialBtn;
foreach(UBActionButton* bt, qlBttns)
{
if(bt->pos().x() <= p.x() &&
bt->pos().x() + bt->rect().width() >= p.x() &&
bt->pos().y() <= p.y() &&
bt->pos().y() + bt->rect().height() >= p.y())
{
if(bt->isVisible())
{
return bt;
}
}
}
// No interesting button has been found
return NULL;
}
/**
* \brief Handles the text changed event of the search bar
*/
void UBLibActionBar::onSearchTextChanged(QString txt)
{
Q_UNUSED(txt);
onActionSearch();
}
/**
* \brief Add a new folder
*/
void UBLibActionBar::onActionNewFolder()
{
emit newFolderToCreate();
}
/**
* \brief Update the action bar elements
* @param crntElem as the current element
*/
void UBLibActionBar::onNavigbarUpdate(UBLibElement *crntElem)
{
if(NULL != crntElem)
{
if(crntElem->type() == eUBLibElementType_Folder)
{
if(libraryController()->canItemsOnElementBeDeleted(crntElem) && !bIsInTrash)
mpNewFolderBtn->setEnabled(true);
else
mpNewFolderBtn->setEnabled(false);
}
else
{
mpNewFolderBtn->setEnabled(false);
}
}
}
void UBLibActionBar::onItemChanged()
{
mSearchBar->setText("");
mpSearchBtn->setEnabled(false);
}
/**
* \brief Construtor
* @param parent as the parent widget
* @param action as the related action
* @param name as the related object name
*/
UBActionButton::UBActionButton(QWidget *parent, QAction* action, const char *name):QToolButton(parent)
{
setObjectName(name);
addAction(action);
setDefaultAction(action);
setIconSize(QSize(BUTTON_SIZE, BUTTON_SIZE));
setToolButtonStyle(Qt::ToolButtonIconOnly);
setStyleSheet(QString("QToolButton {color: white; font-weight: bold; font-family: Arial; background-color: transparent; border: none}"));
setFocusPolicy(Qt::NoFocus);
}
/**
* \brief Destructor
*/
UBActionButton::~UBActionButton()
{
}