-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelegate.cpp
145 lines (130 loc) · 4.72 KB
/
delegate.cpp
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
/*
* desktop-file-memos: A desktop classification app on Linux.
*
* Copyright (C) 2019, Tianjin KYLIN Information Technology Co., Ltd.
*
* 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
* 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/>.
*
* Author: Yue Lan<[email protected]>
*
*/
#include "delegate.h"
#include "fileoperationjob.h"
#include <QTextEdit>
#include <QStaticText>
#include <QFileInfo>
#include <QFile>
#include <QDir>
#include <glib.h>
#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>
#include <QThread>
#include <QDebug>
Delegate::Delegate() {
}
QString Delegate::displayText(const QVariant &value, const QLocale &locale) const {
QString text = value.toString();
if (!text.endsWith(QString(".desktop"))) {
return text;
}
QString keyWord = "Name["+locale.name()+"]";
std::string tmp_key = keyWord.toStdString();
QString filePath = mModel->filePath(mIconView->rootIndex()) + "/" + text;
std::string tmp_str = filePath.toStdString();
const char* file_path = tmp_str.c_str();
GDesktopAppInfo *desktop_app_info = g_desktop_app_info_new_from_filename(file_path);
if (desktop_app_info != nullptr) {
char* tmp_name = g_desktop_app_info_get_string(desktop_app_info, tmp_key.c_str());
if (tmp_name != nullptr) {
text = QString(tmp_name);
g_free (tmp_name);
} else {
tmp_name = g_desktop_app_info_get_string(desktop_app_info, "Name");
if (tmp_name != nullptr) {
text = QString(tmp_name);
g_free (tmp_name);
}
}
g_object_unref(desktop_app_info);
}
return text;
}
QWidget *Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
/*
QLabel *lable = new QLabel(parent);
lable->setText(index.data().toString());
lable->setFixedWidth(108);
lable->setStyleSheet("background-color:white;"
"color:black;");
lable->setWordWrap(true);
//lable->resize(option.rect.size());
qDebug()<<option.rect;
lable->adjustSize();
qDebug()<<lable->rect();
lable->setFixedHeight(200);
return lable;
*/
//this is not good enough.
QTextEdit *textEdit = new QTextEdit(parent);
//textEdit->setWordWrapMode();
//textEdit->setAttribute(Qt::WA_TranslucentBackground,false);
textEdit->setStyleSheet("background-color:white;"
"color:black;");
textEdit->setLineWrapMode(QTextEdit::WidgetWidth);
textEdit->setAlignment(Qt::AlignHCenter);
textEdit->setText(index.data().toString());
textEdit->setFixedWidth(option.rect.width());
textEdit->setFixedHeight(150);
//textEdit->adjustSize();
/*
QStaticText text;
text.setText(index.data().toString());
QTextOption textOption;
textOption.setAlignment(Qt::AlignHCenter);
textOption.setWrapMode(QTextOption::WordWrap);
text.setTextOption(textOption);
text.setTextWidth(108);
//text.prepare();
*/
//qDebug()<<text.size();
//qDebug()<<textEdit->rect()<<option.rect;
connect(textEdit, &QTextEdit::destroyed, [=](){
//qDebug()<<"textEdit destroyed";
//qDebug()<<index.data();
//qDebug()<<textEdit->toPlainText();
//qDebug()<<mModel->filePath(mIconView->rootIndex());
if (!textEdit->toPlainText().isEmpty()) {
QString srcPath = mModel->filePath(mIconView->rootIndex())+"/"+index.data().toString();
QString destPath = mModel->filePath(mIconView->rootIndex())+"/"+textEdit->toPlainText();
QFileInfo info(srcPath);
if (info.isDir()) {
QDir dir;
dir.rename(srcPath,destPath);
} else {
QFile file;
file.rename(srcPath,destPath);
}
}
});
return textEdit;
}
void Delegate::setEditorData(QWidget *, const QModelIndex &) const {
return;
}
void Delegate::setModelData(QWidget *, QAbstractItemModel *, const QModelIndex &) const {
return;
}
void Delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
return QStyledItemDelegate::updateEditorGeometry(editor,option,index);
}