UBFeaturesActionBar.cpp 10.4 KB
Newer Older
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
25 26 27
 */


28 29
#include <QButtonGroup>
#include <QAction>
Claudio Valerio's avatar
Claudio Valerio committed
30

31
#include "UBFeaturesActionBar.h"
32
#include "core/memcheck.h"
33
#include "gui/UBFeaturesWidget.h"
34

35
UBFeaturesActionBar::UBFeaturesActionBar( UBFeaturesController *controller, QWidget* parent, const char* name ) : QWidget (parent)
Claudio Valerio's avatar
Claudio Valerio committed
36
    , featuresController(controller)
37 38 39 40 41
    , mButtonGroup(NULL)
    , mSearchBar(NULL)
    , mLayout(NULL)
    , mpFavoriteAction(NULL)
    , mpSocialAction(NULL)
42
    , mpRescanModelAction(NULL)
43 44 45 46 47 48 49
    , mpDeleteAction(NULL)
    , mpSearchAction(NULL)
    , mpCloseAction(NULL)
    , mpRemoveFavorite(NULL)
    , mpNewFolderAction(NULL)
    , mpFavoriteBtn(NULL)
    , mpSocialBtn(NULL)
50
    , mpRescanModelBtn(NULL)
51 52 53 54
    , mpDeleteBtn(NULL)
    , mpCloseBtn(NULL)
    , mpRemoveFavoriteBtn(NULL)
    , mpNewFolderBtn(NULL)
55
{
Claudio Valerio's avatar
Claudio Valerio committed
56
    setObjectName(name);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    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;"));

    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);
74
    mpRescanModelAction = new QAction(QIcon(":/images/cursors/rotate.png"), tr("Rescan file system"), this);
75 76 77 78 79 80 81 82
    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);
Ilia Ryabokon's avatar
Ilia Ryabokon committed
83

84
    //mpSearchBtn = new UBActionButton(this, mpSearchAction);
85
    mpRescanModelBtn = new UBActionButton(this, mpRescanModelAction);
86
    mpRescanModelBtn->hide();
Ilia Ryabokon's avatar
Ilia Ryabokon committed
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    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
106 107
    /*connect(mpFavoriteAction,SIGNAL(triggered()), this, SLOT(onActionFavorite()));
    connect(mpSocialAction,SIGNAL(triggered()), this, SLOT(onActionSocial()));
108 109 110 111 112 113 114
    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()));*/

115
    connect(mpFavoriteAction,SIGNAL(triggered()), this, SLOT(onActionFavorite()));
Claudio Valerio's avatar
Claudio Valerio committed
116 117
    connect(mSearchBar, SIGNAL(textChanged(QString)), this, SLOT(onSearchTextChanged(QString)));
    connect(mpNewFolderAction, SIGNAL(triggered()), this, SLOT(onActionNewFolder()));
118
    connect(mpRemoveFavorite, SIGNAL(triggered()), this, SLOT(onActionRemoveFavorite()));
119
    connect(mpRescanModelAction, SIGNAL(triggered()), this , SLOT(onActionRescanModel()));
120
    connect(mpDeleteAction,SIGNAL(triggered()), this, SLOT(onActionTrash()));
121

122

123 124 125 126 127 128
    // Build the default toolbar
    mLayout->addWidget(mpFavoriteBtn);
    mLayout->addWidget(mpSocialBtn);
    mLayout->addWidget(mpNewFolderBtn);
    mLayout->addWidget(mSearchBar);
    //mLayout->addWidget(mpSearchBtn);
129
    mLayout->addWidget(mpRescanModelBtn);
130 131 132
    mLayout->addWidget(mpDeleteBtn);
    mLayout->addWidget(mpCloseBtn);
    mLayout->addWidget(mpRemoveFavoriteBtn);
Claudio Valerio's avatar
Claudio Valerio committed
133 134 135
    setCurrentState( IN_ROOT );
    mpDeleteBtn->setAcceptDrops(true);
    setAcceptDrops( true );
136 137 138 139
}

void UBFeaturesActionBar::setCurrentState( UBFeaturesActionBarState state )
{
Claudio Valerio's avatar
Claudio Valerio committed
140 141
    currentState = state;
    setButtons();
142 143 144 145 146 147
}

void UBFeaturesActionBar::setButtons()
{
    switch( currentState )
    {
Claudio Valerio's avatar
Claudio Valerio committed
148 149
    case IN_FOLDER:
        mpFavoriteBtn->show();
150 151 152 153 154 155
        mpSocialBtn->hide();
        mSearchBar->show();
        mpDeleteBtn->show();
        mpCloseBtn->hide();
        mpRemoveFavoriteBtn->hide();
        mpNewFolderBtn->show();
Claudio Valerio's avatar
Claudio Valerio committed
156 157
        mpNewFolderBtn->setEnabled(true);
        mpDeleteBtn->setEnabled(true);
158
//        mpRescanModelBtn->show();
Claudio Valerio's avatar
Claudio Valerio committed
159
        break;
160 161 162 163 164 165 166 167
    case IN_ROOT:
        mpFavoriteBtn->show();
        mpSocialBtn->hide();
        mSearchBar->show();
        mpDeleteBtn->show();
        mpCloseBtn->hide();
        mpRemoveFavoriteBtn->hide();
        mpNewFolderBtn->show();
Claudio Valerio's avatar
Claudio Valerio committed
168 169
        mpNewFolderBtn->setEnabled(false);
        mpDeleteBtn->setEnabled(false);
170
//        mpRescanModelBtn->show();
171 172 173 174 175 176 177 178 179 180
        break;
    case IN_PROPERTIES:
        mpFavoriteBtn->show();
        mpSocialBtn->hide();
        mSearchBar->show();
        //mpSearchBtn->show();
        mpDeleteBtn->hide();
        mpCloseBtn->hide();
        mpRemoveFavoriteBtn->hide();
        mpNewFolderBtn->hide();
181
//        mpRescanModelBtn->hide();
182 183 184 185 186 187 188 189 190 191
        break;
    case IN_FAVORITE:
        mpFavoriteBtn->hide();
        mpSocialBtn->hide();
        mSearchBar->show();
        //mpSearchBtn->show();
        mpDeleteBtn->hide();
        mpCloseBtn->hide();
        mpRemoveFavoriteBtn->show();
        mpNewFolderBtn->hide();
192
//        mpRescanModelBtn->hide();
193
        break;
Claudio Valerio's avatar
Claudio Valerio committed
194 195
    case IN_TRASH:
        mpFavoriteBtn->hide();
Anna Udovichenko's avatar
Anna Udovichenko committed
196 197
        mpSocialBtn->hide();
        mSearchBar->show();
198 199
        mpDeleteBtn->show();
        mpDeleteBtn->setEnabled(true);
Anna Udovichenko's avatar
Anna Udovichenko committed
200 201 202 203 204
        //mpSearchBtn->show();
        //mpDeleteBtn->hide();
        mpCloseBtn->hide();
        //mpRemoveFavoriteBtn->show();
        mpNewFolderBtn->hide();
205
//        mpRescanModelBtn->hide();
Claudio Valerio's avatar
Claudio Valerio committed
206
        break;
207 208 209 210 211 212 213
    default:
        break;
    }
}

void UBFeaturesActionBar::onSearchTextChanged(QString txt)
{
Ivan Ilyin's avatar
Ivan Ilyin committed
214
    Q_UNUSED(txt)
215
    emit searchElement(mSearchBar->text());
216 217
}
   
218 219 220 221 222
void UBFeaturesActionBar::onActionNewFolder()
{
    emit newFolderToCreate();
}

223 224 225 226 227
void UBFeaturesActionBar::onActionFavorite()
{
    emit addElementsToFavorite();
}

228 229 230 231 232
void UBFeaturesActionBar::onActionRemoveFavorite()
{
    emit removeElementsFromFavorite();
}

233 234 235 236
void UBFeaturesActionBar::onActionTrash()
{
    emit deleteSelectedElements();
}
237
void UBFeaturesActionBar::onActionRescanModel()
238
{
239
    emit rescanModel();
240 241
}

242 243 244 245 246 247 248 249 250 251
void UBFeaturesActionBar::lockIt()
{
    setEnabled(false);
}

void UBFeaturesActionBar::unlockIt()
{
    setEnabled(true);
}

252 253
void UBFeaturesActionBar::dragEnterEvent( QDragEnterEvent *event )
{
254 255
    const UBFeaturesMimeData *fMimeData = qobject_cast<const UBFeaturesMimeData*>(event->mimeData());
    if (fMimeData) {
256
        event->acceptProposedAction();
257 258 259
    } else {
        event->ignore();
    }
260 261
}

262
void UBFeaturesActionBar::dropEvent(QDropEvent *event)
Claudio Valerio's avatar
Claudio Valerio committed
263
{    
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    const UBFeaturesMimeData *fMimeData = qobject_cast<const UBFeaturesMimeData*>(event->mimeData());

    if (!fMimeData) {
        qWarning() << "data came from not supported widget";
        event->ignore();
        return;
    }

    QWidget *dest = childAt(event->pos());
    if (dest == mpDeleteBtn) {
        QList<UBFeature> featuresList = fMimeData->features();
        foreach (UBFeature curFeature, featuresList) {
            if (!curFeature.isDeletable()) {
                qWarning() << "Undeletable feature found, stopping deleting process";
                event->ignore();
                return;
            }
        }
        event->setDropAction(Qt::MoveAction);
Claudio Valerio's avatar
Claudio Valerio committed
283
        event->accept();
284 285 286 287 288

        emit deleteElements(fMimeData);

    } else if (dest == mpFavoriteBtn) {
        event->setDropAction( Qt::CopyAction);
Claudio Valerio's avatar
Claudio Valerio committed
289
        event->accept();
290 291 292 293

        emit addToFavorite(fMimeData);

    } else if (dest == mpRemoveFavoriteBtn) {
Claudio Valerio's avatar
Claudio Valerio committed
294 295
        event->setDropAction( Qt::MoveAction );
        event->accept();
296 297

        emit removeFromFavorite(fMimeData);
Claudio Valerio's avatar
Claudio Valerio committed
298
    }
299 300
}

301 302
UBFeaturesActionBar::~UBFeaturesActionBar()
{
Ivan Ilyin's avatar
Ivan Ilyin committed
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

/**
 * \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()
{

}