Icon view can be renamed

This commit is contained in:
cutefishd 2021-03-31 09:22:32 +08:00
parent 655fff6e7a
commit 5618819563
7 changed files with 118 additions and 19 deletions

View file

@ -123,8 +123,9 @@ QVariant FolderModel::data(const QModelIndex &index, int role) const
return m_selectionModel->isSelected(index);
case UrlRole:
return item.url();
case FileNameRole:
case FileNameRole: {
return item.url().fileName();
}
case IconNameRole:
return item.iconName();
case ThumbnailRole: {
@ -932,7 +933,7 @@ bool FolderModel::isSupportThumbnails(const QString &mimeType) const
{
const QStringList supportsMimetypes = {"image/bmp", "image/png", "image/gif", "image/jpeg", "image/web",
"application/pdf", "application/rtf", "application/doc", "application/odf",
"audio/mpeg"};
"audio/mpeg", "video/mp4"};
if (supportsMimetypes.contains(mimeType))
return true;

View file

@ -87,7 +87,6 @@ PlacesModel::PlacesModel(QObject *parent)
trashItem->setIconPath("qrc:/images/user-trash.svg");
m_items.append(trashItem);
Solid::Predicate predicate;
QString predicateStr(
QString::fromLatin1("[[[[ StorageVolume.ignored == false AND [ StorageVolume.usage == 'FileSystem' OR StorageVolume.usage == 'Encrypted' ]]"
" OR "
@ -96,14 +95,14 @@ PlacesModel::PlacesModel(QObject *parent)
"OpticalDisc.availableContent & 'Audio' ]"
" OR "
"StorageAccess.ignored == false ]"));
predicate = Solid::Predicate::fromString(predicateStr);
m_predicate = Solid::Predicate::fromString(predicateStr);
Solid::DeviceNotifier *notifier = Solid::DeviceNotifier::instance();
connect(notifier, &Solid::DeviceNotifier::deviceAdded, this, &PlacesModel::onDeviceAdded);
connect(notifier, &Solid::DeviceNotifier::deviceRemoved, this, &PlacesModel::onDeviceRemoved);
// Init devices
const QList<Solid::Device> &deviceList = Solid::Device::listFromQuery(predicate);
const QList<Solid::Device> &deviceList = Solid::Device::listFromQuery(m_predicate);
for (const Solid::Device &device : deviceList) {
PlacesItem *deviceItem = new PlacesItem;
deviceItem->setUdi(device.udi());
@ -239,11 +238,13 @@ void PlacesModel::requestEject(const int &index)
void PlacesModel::onDeviceAdded(const QString &udi)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
PlacesItem *deviceItem = new PlacesItem;
deviceItem->setUdi(udi);
m_items.append(deviceItem);
endInsertRows();
if (m_predicate.matches(Solid::Device(udi))) {
beginInsertRows(QModelIndex(), rowCount(), rowCount());
PlacesItem *deviceItem = new PlacesItem;
deviceItem->setUdi(udi);
m_items.append(deviceItem);
endInsertRows();
}
}
void PlacesModel::onDeviceRemoved(const QString &udi)

View file

@ -21,6 +21,7 @@
#define PLACESMODEL_H
#include <QAbstractItemModel>
#include <Solid/Predicate>
#include "placesitem.h"
class PlacesModel : public QAbstractItemModel
@ -61,6 +62,7 @@ private slots:
private:
QList<PlacesItem *> m_items;
Solid::Predicate m_predicate;
};
#endif

View file

@ -24,15 +24,14 @@ Item {
Rectangle {
id: _background
width: Math.max(_iconItem.width + Meui.Units.smallSpacing,
_label.paintedWidth + Meui.Units.largeSpacing)
width: Math.max(_iconItem.width, _label.paintedWidth)
height: _iconItem.height + _label.paintedHeight + Meui.Units.largeSpacing
x: (parent.width - width) / 2
y: _iconItem.y
color: selected || hovered ? Meui.Theme.highlightColor : "transparent"
radius: Meui.Theme.mediumRadius
visible: selected || hovered
opacity: selected ? 0.9 : 0.4
opacity: selected ? 1.0 : 0.4
}
Item {
@ -45,7 +44,6 @@ Item {
width: parent.width - Meui.Units.largeSpacing * 2
height: control.GridView.view.iconSize
opacity: selected || !hovered ? 1.0 : 0.95
Image {
id: _icon

View file

@ -41,6 +41,25 @@ GridView {
signal keyPress(var event)
function rename() {
if (control.currentIndex != -1) {
var renameAction = control.model.action("rename")
if (renameAction && !renameAction.enabled)
return
if (!editor)
editor = editorComponent.createObject(control)
editor.targetItem = control.currentItem
}
}
function cancelRename() {
if (editor) {
editor.targetItem = null;
}
}
highlightMoveDuration: 0
keyNavigationEnabled : true
keyNavigationWraps : true
@ -115,6 +134,9 @@ GridView {
onPressed: {
control.forceActiveFocus()
if (control.editor && childAt(mouse.x, mouse.y) !== control.editor)
control.editor.commit()
pressX = mouse.x
pressY = mouse.y
@ -353,4 +375,68 @@ GridView {
}
return Math.floor(extraSpacing)
}
Component {
id: editorComponent
TextField {
id: _editor
visible: false
wrapMode: TextEdit.Wrap
horizontalAlignment: TextEdit.AlignHCenter
z: 999
property Item targetItem: null
onTargetItemChanged: {
if (targetItem != null) {
var pos = control.mapFromItem(targetItem, targetItem.textArea.x, targetItem.textArea.y)
x = targetItem.x + Math.abs(Math.min(control.contentX, control.originX))
y = pos.y + Meui.Units.smallSpacing
width = targetItem.width - Meui.Units.smallSpacing
height = Meui.Units.fontMetrics.height * 2
text = targetItem.textArea.text
targetItem.textArea.visible = false
_editor.select(0, folderModel.fileExtensionBoundary(targetItem.index))
visible = true
} else {
x: 0
y: 0
visible = false
}
}
Keys.onPressed: {
switch (event.key) {
case Qt.Key_Return:
case Qt.Key_Enter:
commit()
break
case Qt.Key_Escape:
if (targetItem) {
targetItem.textArea.visible = true
targetItem = null
event.accepted = true
}
break
}
}
onVisibleChanged: {
if (visible)
_editor.forceActiveFocus()
else
control.forceActiveFocus()
}
function commit() {
if (targetItem) {
targetItem.textArea.visible = true
folderModel.rename(targetItem.index, text)
control.currentIndex = targetItem.index
targetItem = null
}
}
}
}
}

View file

@ -75,6 +75,14 @@ Item {
}
}
function rename() {
_viewLoader.item.rename()
}
Component.onCompleted: {
folderModel.requestRename.connect(rename)
}
Component {
id: _statusBar
@ -121,6 +129,10 @@ Item {
onCountChanged: {
_fileTips.visible = count === 0
}
// Component.onCompleted: {
// folderModel.requestRename.connect(rename)
// }
}
}
@ -140,10 +152,6 @@ Item {
}
delegate: FolderListItem {}
Component.onCompleted: {
folderModel.requestRename.connect(rename)
}
}
}

View file

@ -20,9 +20,12 @@ ListView {
leftMargin: Meui.Units.smallSpacing
rightMargin: Meui.Units.smallSpacing
bottomMargin: Meui.Units.smallSpacing
spacing: Meui.Units.largeSpacing
ScrollBar.vertical: ScrollBar {}
ScrollBar.vertical: ScrollBar {
bottomPadding: Meui.Units.smallSpacing
}
highlightFollowsCurrentItem: true
highlightMoveDuration: 0