| 1 |
/* |
| 2 |
* Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. |
| 3 |
* |
| 4 |
* Author: ZhangYong <[email protected]> |
| 5 |
* |
| 6 |
* Maintainer: ZhangYong <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
#include "application.h" |
| 22 |
#include "albumcreatedialog.h" |
| 23 |
#include "dbmanager/dbmanager.h" |
| 24 |
#include "utils/baseutils.h" |
| 25 |
#include "ac-desktop-define.h" |
| 26 |
#include <QHBoxLayout> |
| 27 |
#include <QLabel> |
| 28 |
#include <DLineEdit> |
| 29 |
#include <QKeyEvent> |
| 30 |
#include <QDebug> |
| 31 |
#include <DPushButton> |
| 32 |
#include <DFontSizeManager> |
| 33 |
#include <DApplicationHelper> |
| 34 |
#include <DSuggestButton> |
| 35 |
|
| 36 |
AlbumCreateDialog::AlbumCreateDialog(DWidget *parent) |
| 37 |
: DDialog(parent), edit(nullptr), m_OKClicked(false) |
| 38 |
{ |
| 39 |
initUI(); |
| 40 |
initConnection(); |
| 41 |
getEdit(); |
| 42 |
} |
| 43 |
|
| 44 |
void AlbumCreateDialog::keyPressEvent(QKeyEvent *e) |
| 45 |
{ |
| 46 |
if (e->key() == Qt::Key_Escape) { |
| 47 |
emit sigClose(); |
| 48 |
this->close(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
bool AlbumCreateDialog::eventFilter(QObject *obj, QEvent *event) |
| 53 |
{ |
| 54 |
if (event->type() == QEvent::KeyPress) { |
| 55 |
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); |
| 56 |
if (keyEvent->key() == Qt::Key_Tab) { |
| 57 |
// 修改编辑框焦点方式 |
| 58 |
edit->lineEdit()->setFocusPolicy(Qt::ClickFocus); |
| 59 |
// cancel sure btn tab foucs |
| 60 |
if (getButton(0) == obj) { |
| 61 |
getButton(1)->setFocus(); |
| 62 |
return true; |
| 63 |
} else if (getButton(1) == obj) { |
| 64 |
m_allTabOrder.at(3)->setFocus(); |
| 65 |
return true; |
| 66 |
} else if (obj->objectName() == "DTitlebarDWindowCloseButton") { |
| 67 |
edit->lineEdit()->setFocus(); |
| 68 |
edit->lineEdit()->selectAll(); |
| 69 |
return true; |
| 70 |
} |
| 71 |
} else if (keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right |
| 72 |
|| keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
} |
| 76 |
return QWidget::eventFilter(obj, event); |
| 77 |
} |
| 78 |
|
| 79 |
void AlbumCreateDialog::initUI() |
| 80 |
{ |
| 81 |
setFixedSize(380, 180); |
| 82 |
setModal(true); |
| 83 |
setContentsMargins(0, 0, 0, 0); |
| 84 |
setSpacing(0); |
| 85 |
//内容widget |
| 86 |
DWidget *contentWidget = new DWidget(this); |
| 87 |
contentWidget->setContentsMargins(0, 0, 0, 0); |
| 88 |
|
| 89 |
//图标 |
| 90 |
DLabel *logoLable = new DLabel(this); |
| 91 |
QIcon icon = QIcon::fromTheme("deepin-album"); |
| 92 |
logoLable->setPixmap(icon.pixmap(QSize(32, 32))); |
| 93 |
logoLable->move(10, 9); |
| 94 |
logoLable->setAlignment(Qt::AlignLeft); |
| 95 |
//title |
| 96 |
DLabel *title = new DLabel(this); |
| 97 |
DFontSizeManager::instance()->bind(title, DFontSizeManager::T6, QFont::DemiBold); |
| 98 |
title->setForegroundRole(DPalette::TextTitle); |
| 99 |
title->setText(tr("New Album")); |
| 100 |
title->setFixedSize(293, 30); |
| 101 |
title->setObjectName("DialogTitle"); |
| 102 |
title->setAlignment(Qt::AlignHCenter); |
| 103 |
title->move(44, 49); |
| 104 |
|
| 105 |
//编辑框 |
| 106 |
edit = new DLineEdit(this); |
| 107 |
edit->setEnabled(true); |
| 108 |
edit->setObjectName("DialogEdit"); |
| 109 |
edit->setText(getNewAlbumName("")); |
| 110 |
edit->setContextMenuPolicy(Qt::PreventContextMenu); |
| 111 |
edit->setClearButtonEnabled(false); |
| 112 |
edit->setFixedSize(360, 36); |
| 113 |
edit->move(10, 79); |
| 114 |
DFontSizeManager::instance()->bind(edit, DFontSizeManager::T6, QFont::DemiBold); |
| 115 |
addButton(tr("Cancel"), false, DDialog::ButtonNormal); |
| 116 |
addButton(tr("Create"), true, DDialog::ButtonRecommend); |
| 117 |
getButton(0)->setFixedSize(170, 35); |
| 118 |
getButton(1)->setFixedSize(170, 35); |
| 119 |
connect(edit, &DLineEdit::textEdited, this, &AlbumCreateDialog::onTextEdited); |
| 120 |
addContent(contentWidget); |
| 121 |
QWidget *closeButton = this->findChild<QWidget *>("DTitlebarDWindowCloseButton"); |
| 122 |
closeButton->setFocusPolicy(Qt::TabFocus); |
| 123 |
//键盘交互 |
| 124 |
getButton(0)->installEventFilter(this); |
| 125 |
getButton(1)->installEventFilter(this); |
| 126 |
closeButton->installEventFilter(this); |
| 127 |
edit->installEventFilter(this); |
| 128 |
|
| 129 |
m_allTabOrder.clear(); |
| 130 |
m_allTabOrder.insert(0, edit); |
| 131 |
m_allTabOrder.insert(1, getButton(0)); |
| 132 |
m_allTabOrder.insert(2, getButton(1)); |
| 133 |
m_allTabOrder.insert(3, closeButton); |
| 134 |
} |
| 135 |
|
| 136 |
void AlbumCreateDialog::initConnection() |
| 137 |
{ |
| 138 |
connect(this, &AlbumCreateDialog::visibleChanged, this, &AlbumCreateDialog::onVisibleChanged); |
| 139 |
connect(edit, &DLineEdit::returnPressed, this, &AlbumCreateDialog::onReturnPressed); |
| 140 |
connect(this, &AlbumCreateDialog::buttonClicked, this, &AlbumCreateDialog::onButtonClicked); |
| 141 |
connect(this, &AlbumCreateDialog::closed, this, &AlbumCreateDialog::onClosed); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* @brief AlbumCreateDialog::getNewAlbumName |
| 146 |
* @param[in] baseName |
| 147 |
* @param[in] isWithOutSelf |
| 148 |
* @param[in] beforeName |
| 149 |
* @author DJH |
| 150 |
* @date 2020/6/1 |
| 151 |
* @return const QString |
| 152 |
* 根据已有相册名,获取对于数据库中不重复的新相册名,当isWithOutSefl为true的时候,查询不会包含自己,用于替换型查询 |
| 153 |
*/ |
| 154 |
const QString AlbumCreateDialog::getNewAlbumName(const QString &baseName, bool isWithOutSelf, const QString &beforeName) |
| 155 |
{ |
| 156 |
QString nan; |
| 157 |
QString albumName; |
| 158 |
int num = 1; |
| 159 |
if (baseName.isEmpty()) { |
| 160 |
nan = tr("Unnamed"); |
| 161 |
albumName = nan; |
| 162 |
} else { |
| 163 |
nan = baseName; |
| 164 |
albumName = nan + QString::number(num); |
| 165 |
} |
| 166 |
QStringList albums = DBManager::instance()->getAllAlbumNames(); |
| 167 |
if (isWithOutSelf) { |
| 168 |
albums.removeOne(beforeName); |
| 169 |
} |
| 170 |
|
| 171 |
while (albums.contains(albumName)) { |
| 172 |
albumName = nan + QString::number(num); |
| 173 |
num++; |
| 174 |
} |
| 175 |
return static_cast<const QString>(albumName); |
| 176 |
} |
| 177 |
|
| 178 |
DLineEdit *AlbumCreateDialog::getEdit() |
| 179 |
{ |
| 180 |
if (edit != nullptr) |
| 181 |
return edit; |
| 182 |
return nullptr; |
| 183 |
} |
| 184 |
|
| 185 |
const QString AlbumCreateDialog::getCreateAlbumName() const |
| 186 |
{ |
| 187 |
return m_createAlbumName; |
| 188 |
} |
| 189 |
|
| 190 |
void AlbumCreateDialog::createAlbum(const QString &newName) |
| 191 |
{ |
| 192 |
if (!DBManager::instance()->isAlbumExistInDB(newName)) { |
| 193 |
m_createAlbumName = newName; |
| 194 |
DBManager::instance()->insertIntoAlbum(newName, QStringList(" ")); |
| 195 |
} else { |
| 196 |
m_createAlbumName = getNewAlbumName(newName); |
| 197 |
DBManager::instance()->insertIntoAlbum(m_createAlbumName, QStringList(" ")); |
| 198 |
} |
| 199 |
|
| 200 |
emit albumAdded(); |
| 201 |
} |
| 202 |
|
| 203 |
void AlbumCreateDialog::onTextEdited(const QString &) |
| 204 |
{ |
| 205 |
if (edit->text().trimmed().isEmpty()) { |
| 206 |
getButton(1)->setEnabled(false); |
| 207 |
} else { |
| 208 |
getButton(1)->setEnabled(true); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
void AlbumCreateDialog::onVisibleChanged(bool v) |
| 213 |
{ |
| 214 |
if (! v) return; |
| 215 |
edit->lineEdit()->selectAll(); |
| 216 |
edit->lineEdit()->setFocus(); |
| 217 |
} |
| 218 |
|
| 219 |
void AlbumCreateDialog::onReturnPressed() |
| 220 |
{ |
| 221 |
const QString album = edit->text().trimmed(); |
| 222 |
if (! album.isEmpty()) { |
| 223 |
createAlbum(album); |
| 224 |
this->close(); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
void AlbumCreateDialog::onButtonClicked(int index) |
| 229 |
{ |
| 230 |
if (0 == index) { |
| 231 |
deleteLater(); |
| 232 |
emit sigClose(); |
| 233 |
} |
| 234 |
if (1 == index) { |
| 235 |
if (edit->text().simplified().length() != 0) { |
| 236 |
createAlbum(edit->text().trimmed()); |
| 237 |
} else { |
| 238 |
QString str = tr("Unnamed") + QString::number(1); |
| 239 |
createAlbum(str); |
| 240 |
} |
| 241 |
m_OKClicked = true; |
| 242 |
this->close(); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
void AlbumCreateDialog::onClosed() |
| 247 |
{ |
| 248 |
deleteLater(); |
| 249 |
if (true == m_OKClicked) { |
| 250 |
m_OKClicked = false; |
| 251 |
} else { |
| 252 |
emit sigClose(); |
| 253 |
} |
| 254 |
} |