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
|
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright: 2023 LXQt team
* Authors:
* Filippo Gentile <filippogentile@disroot.org>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "lxqtfancymenuappmodel.h"
#include "lxqtfancymenuappmap.h"
#include <QMimeData>
#include <QUrl>
static const QLatin1String FavoritesDragMimeType("application/x-lxqtfavoritesdragrow");
LXQtFancyMenuAppModel::LXQtFancyMenuAppModel(QObject *parent)
: QAbstractListModel(parent)
, mAppMap(nullptr)
, mCurrentCategory(0)
, mInSearch(false)
{
}
int LXQtFancyMenuAppModel::rowCount(const QModelIndex &p) const
{
if(!mAppMap || p.isValid() || mCurrentCategory < 0 || mCurrentCategory >= mAppMap->getCategoriesCount())
return 0;
if(mInSearch)
return mSearchMatches.size();
if(mCurrentCategory == LXQtFancyMenuAppMap::AllAppsCategory)
return mAppMap->getTotalAppCount(); //Special "All Applications" category
return mAppMap->getCategoryAt(mCurrentCategory).apps.size();
}
QVariant LXQtFancyMenuAppModel::data(const QModelIndex &idx, int role) const
{
if(!idx.isValid())
return QVariant();
const LXQtFancyMenuAppMap::AppItem* item = getAppAt(idx.row());
LXQtFancyMenuItemType type = getItemTypeAt(idx.row());
if(!item && type == LXQtFancyMenuItemType::AppItem)
return QVariant();
if(!item)
{
if(role == LXQtFancyMenuItemIsSeparatorRole)
return 1;
return QVariant();
}
switch (role)
{
case Qt::DisplayRole:
return item->title;
case Qt::EditRole:
return item->desktopFile;
case Qt::DecorationRole:
return item->icon;
case Qt::ToolTipRole:
{
return item->comment;
}
default:
break;
}
return QVariant();
}
Qt::ItemFlags LXQtFancyMenuAppModel::flags(const QModelIndex &idx) const
{
Qt::ItemFlags f = QAbstractListModel::flags(idx);
if(mCurrentCategory == LXQtFancyMenuAppMap::FavoritesCategory)
f.setFlag(Qt::ItemIsDropEnabled); //Allow drag-drop of favorites
const LXQtFancyMenuAppMap::AppItem* item = getAppAt(idx.row());
LXQtFancyMenuItemType type = getItemTypeAt(idx.row());
if(!item || type == LXQtFancyMenuItemType::SeparatorItem)
return f;
if (idx.isValid())
f.setFlag(Qt::ItemIsDragEnabled);
return f;
}
QStringList LXQtFancyMenuAppModel::mimeTypes() const
{
return {FavoritesDragMimeType};
}
QMimeData *LXQtFancyMenuAppModel::mimeData(const QModelIndexList &indexes) const
{
QList<QUrl> urls;
int row = -1;
for(const QModelIndex& idx : indexes)
{
const LXQtFancyMenuAppMap::AppItem* item = getAppAt(idx.row());
if(!item)
continue;
urls << QUrl::fromLocalFile(item->desktopFile);
if(row == -1)
row = idx.row();
}
QMimeData *mimeData = new QMimeData();
mimeData->setUrls(urls);
if(row != -1)
mimeData->setData(FavoritesDragMimeType, QByteArray::number(row));
return mimeData;
}
bool LXQtFancyMenuAppModel::dropMimeData(const QMimeData *data_, Qt::DropAction /*action*/,
int row, int /*column*/, const QModelIndex &p)
{
if(mCurrentCategory != LXQtFancyMenuAppMap::FavoritesCategory)
return false;
auto urls = data_->urls();
if(urls.isEmpty())
return false;
QString desktopFile =urls.first().toLocalFile();
int oldRow = mAppMap->getFavoriteIndex(desktopFile);
if(oldRow == -1)
return false;
if(row == -1 && p.isValid())
{
// Dropped onto item but this model is a flat list
// If going upwards we drop above destination item
row = p.row();
if(oldRow < p.row())
{
// If going downwards we drop below destination item
row++;
}
}
if(row == -1)
return false;
// Compensate the fact that we first remove the item
// so all indexes are shifted by -1, store original value
int realRow = row;
if(row > oldRow)
row--;
if(row == oldRow)
return false; // No-op
// realRow is needed because beginMoveRows() behaves differenlty than
// QList<...>::move() on index counting.
beginMoveRows(QModelIndex(), oldRow, oldRow, QModelIndex(), realRow);
mAppMap->moveFavoriteItem(oldRow, row);
endMoveRows();
emit favoritesChanged();
return true;
}
Qt::DropActions LXQtFancyMenuAppModel::supportedDragActions() const
{
return Qt::CopyAction | Qt::LinkAction | Qt::MoveAction;
}
void LXQtFancyMenuAppModel::reloadAppMap(bool end)
{
if(!end)
beginResetModel();
else
endResetModel();
}
void LXQtFancyMenuAppModel::setCurrentCategory(int category)
{
beginResetModel();
mCurrentCategory = category;
endResetModel();
}
void LXQtFancyMenuAppModel::showSearchResults(const QList<const LXQtFancyMenuAppItem *> &matches)
{
beginResetModel();
mSearchMatches = matches;
mInSearch = true;
endResetModel();
}
void LXQtFancyMenuAppModel::endSearch()
{
beginResetModel();
mSearchMatches.clear();
mSearchMatches.squeeze();
mInSearch = false;
endResetModel();
}
LXQtFancyMenuAppMap *LXQtFancyMenuAppModel::appMap() const
{
return mAppMap;
}
void LXQtFancyMenuAppModel::setAppMap(LXQtFancyMenuAppMap *newAppMap)
{
mAppMap = newAppMap;
}
const LXQtFancyMenuAppItem *LXQtFancyMenuAppModel::getAppAt(int idx) const
{
if(!mAppMap || idx < 0 || mCurrentCategory < 0 || mCurrentCategory >= mAppMap->getCategoriesCount())
return nullptr;
if(mInSearch)
return mSearchMatches.value(idx, nullptr);
if(mCurrentCategory == LXQtFancyMenuAppMap::AllAppsCategory)
return mAppMap->getAppAt(idx); //Special "All Applications" category
const LXQtFancyMenuAppMap::Category& cat = mAppMap->getCategoryAt(mCurrentCategory);
if(idx >= cat.apps.size())
return nullptr;
const LXQtFancyMenuAppMap::Category::Item& item = cat.apps.at(idx);
return item.appItem;
}
LXQtFancyMenuItemType LXQtFancyMenuAppModel::getItemTypeAt(int idx) const
{
if(!mAppMap || idx < 0 || mCurrentCategory < 0 || mCurrentCategory >= mAppMap->getCategoriesCount())
return LXQtFancyMenuItemType::AppItem;
if(mInSearch)
return LXQtFancyMenuItemType::AppItem;
if(mCurrentCategory == LXQtFancyMenuAppMap::AllAppsCategory)
return LXQtFancyMenuItemType::AppItem; //Special "All Applications" category
const LXQtFancyMenuAppMap::Category& cat = mAppMap->getCategoryAt(mCurrentCategory);
if(idx >= cat.apps.size())
return LXQtFancyMenuItemType::AppItem;
const LXQtFancyMenuAppMap::Category::Item& item = cat.apps.at(idx);
return item.type;
}
bool LXQtFancyMenuAppModel::isInSearch() const
{
return mInSearch;
}
|