UBLibWidget.cpp 5.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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 <QDebug>

#include "UBLibWidget.h"
shibakaneki's avatar
shibakaneki committed
18
#include "core/UBApplication.h"
19

20 21
#include "core/memcheck.h"

22 23 24 25 26 27 28 29 30 31 32
/**
 * \brief Constructor
 * @param parent as the parent widget
 * @param name as the object name
 */
UBLibWidget::UBLibWidget(QWidget *parent, const char *name):UBDockPaletteWidget(parent)
  , mLayout(NULL)
  , mStackedWidget(NULL)
  , mNavigator(NULL)
  , mProperties(NULL)
  , mActionBar(NULL)
33 34
  , mpWebView(NULL)
  , mpPathViewer(NULL)
35 36 37
{
    setObjectName(name);
    mName = "LibWidget";
38
    mVisibleState = true;
shibakaneki's avatar
shibakaneki committed
39 40 41 42

    setAttribute(Qt::WA_StyledBackground, true);
    setStyleSheet(UBApplication::globalStyleSheet());

43 44 45 46 47 48 49
    mIconToLeft = QPixmap(":images/library_open.png");
    mIconToRight = QPixmap(":images/library_close.png");
    setAcceptDrops(true);

    mLayout = new QVBoxLayout(this);
    setLayout(mLayout);

50
    // -------------
51
    // Build the GUI
52 53
    // -------------
    // The 'global' widgets
54 55
    mStackedWidget = new QStackedWidget(this);
    mActionBar = new UBLibActionBar(this);
56 57 58 59
    mpPathViewer = new UBLibPathViewer(this);
    mpPathViewer->setMaximumHeight(62);

    // The internal widgets
60 61
    mNavigator = new UBLibNavigatorWidget(this);
    mProperties = new UBLibItemProperties(this);
62
    mpWebView = new UBLibWebView(this);
63

64
    mLayout->addWidget(mpPathViewer, 0);
65 66 67 68 69
    mLayout->addWidget(mStackedWidget, 1);
    mLayout->addWidget(mActionBar, 0);

    mStackedWidget->addWidget(mNavigator);
    mStackedWidget->addWidget(mProperties);
70
    mStackedWidget->addWidget(mpWebView);
71 72 73 74

    mStackedWidget->setCurrentIndex(ID_NAVIGATOR);
    miCrntStackWidget = ID_NAVIGATOR;

75
    connect(mNavigator, SIGNAL(updateNavigBar(UBChainedLibElement*)), this, SLOT(onUpdateNavigBar(UBChainedLibElement*)));
76
    connect(mNavigator, SIGNAL(propertiesRequested(UBLibElement*)), this, SLOT(showProperties(UBLibElement*)));
77
    connect(mNavigator, SIGNAL(displaySearchEngine(UBLibElement*)), this, SLOT(showSearchEngine(UBLibElement*)));
78
    connect(mProperties, SIGNAL(showFolderContent()), this, SLOT(showFolder()));
79 80
    connect(this, SIGNAL(showLibElemProperties()), mpPathViewer, SLOT(showBack()));
    connect(this, SIGNAL(showLibSearchEngine()), mpPathViewer, SLOT(showBack()));
81 82 83 84 85 86 87
}

/**
 * \brief Destructor
 */
UBLibWidget::~UBLibWidget()
{
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    if(NULL != mpPathViewer)
    {
        delete mpPathViewer;
        mpPathViewer = NULL;
    }
    if(NULL != mNavigator)
    {
        delete mNavigator;
        mNavigator = NULL;
    }
    if(NULL != mpWebView)
    {
        delete mpWebView;
        mpWebView = NULL;
    }
103 104 105 106 107
    if(NULL != mProperties)
    {
        delete mProperties;
        mProperties = NULL;
    }
108 109 110 111 112
    if(NULL != mStackedWidget)
    {
        delete mStackedWidget;
        mStackedWidget = NULL;
    }
113 114 115 116 117
    if(NULL != mActionBar)
    {
        delete mActionBar;
        mActionBar = NULL;
    }
118 119 120 121 122
    if(NULL != mLayout)
    {
        delete mLayout;
        mLayout = NULL;
    }
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
}

/**
 * \brief Handles the drag enter event
 * @param pEvent as the drag enter event
 */
void UBLibWidget::dragEnterEvent(QDragEnterEvent *pEvent)
{
    setBackgroundRole(QPalette::Highlight);
    pEvent->acceptProposedAction();
}

void UBLibWidget::dragLeaveEvent(QDragLeaveEvent *pEvent)
{
    pEvent->accept();
}

/**
 * \brief Handles the drop event
 * @param pEvent as the drop event
 */
void UBLibWidget::dropEvent(QDropEvent *pEvent)
{
    processMimeData(pEvent->mimeData());
    setBackgroundRole(QPalette::Dark);
    mStackedWidget->setCurrentIndex(miCrntStackWidget);
    pEvent->acceptProposedAction();
}

/**
 * \brief Handles the drag move event
 * @param pEvent as the drag move event
 */
void UBLibWidget::dragMoveEvent(QDragMoveEvent *pEvent)
{
    pEvent->acceptProposedAction();
}

/**
 * \brief Process the dropped MIME data
 * @param pData as the mime dropped data
 */
void UBLibWidget::processMimeData(const QMimeData *pData)
{
    // Display the different mime types contained in the mime data
    QStringList qslFormats = pData->formats();
    for(int i = 0; i < qslFormats.size(); i++)
    {
        qDebug() << "Dropped element format " << i << " = "<< qslFormats.at(i);
    }
}

void UBLibWidget::showProperties(UBLibElement *elem)
{
    if(NULL != elem)
    {
179
        emit showLibElemProperties();
180 181 182 183 184 185 186
        mActionBar->setButtons(eButtonSet_Properties);
        mProperties->showElement(elem);
        mStackedWidget->setCurrentIndex(ID_PROPERTIES);
        miCrntStackWidget = ID_PROPERTIES;
    }
}

187 188 189 190 191 192 193 194 195 196 197 198
void UBLibWidget::showSearchEngine(UBLibElement *elem)
{
    if(NULL != elem)
    {
        emit showLibSearchEngine();
        mActionBar->hide();
        mpWebView->setElement(elem);
        mStackedWidget->setCurrentIndex(ID_WEBVIEW);
        miCrntStackWidget = ID_WEBVIEW;
    }
}

199 200
void UBLibWidget::showFolder()
{
201 202 203
    if(!mActionBar->isVisible()){
        mActionBar->setVisible(true);
    }
204 205 206 207 208 209 210 211 212 213 214 215 216 217
    mActionBar->setButtons(mActionBar->previousButtonSet());
    mStackedWidget->setCurrentIndex(ID_NAVIGATOR);
    miCrntStackWidget = ID_NAVIGATOR;
}

int UBLibWidget::customMargin()
{
    return 5;
}

int UBLibWidget::border()
{
    return 15;
}
218 219 220 221 222 223 224 225 226 227 228

void UBLibWidget::onUpdateNavigBar(UBChainedLibElement *elem)
{
    mpPathViewer->displayPath(elem);
    mpPathViewer->show();

    if(ID_NAVIGATOR != miCrntStackWidget)
    {
        showFolder();
    }
}