-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc_image.cpp
195 lines (166 loc) · 6.17 KB
/
func_image.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
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
#include "func_image.h"
#include "ui_func_image.h"
#include <QDir>
#include <QFileInfo>
#include <QListWidgetItem>
#include <QPixmap>
#include <QToolBar>
#include <QAction>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QStatusBar>
#include <QWheelEvent>
func_image::func_image(QWidget *parent) :
QWidget(parent),
ui(new Ui::func_image),
scaleFactor(1.0),
rotationAngle(0)
{
ui->setupUi(this);
// 初始化列表和显示区域
listWidget = new QListWidget(this);
imageLabel = new QLabel(this);
imageLabel->setAlignment(Qt::AlignCenter);
imageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
imageLabel->setScaledContents(false);
// 初始化工具栏
toolBar = new QToolBar(this);
QAction *zoomInAction = toolBar->addAction("Zoom In");
QAction *zoomOutAction = toolBar->addAction("Zoom Out");
QAction *rotateLeftAction = toolBar->addAction("Rotate Left");
QAction *rotateRightAction = toolBar->addAction("Rotate Right");
// 连接工具栏按钮的信号与槽
connect(zoomInAction, &QAction::triggered, this, &func_image::zoomIn);
connect(zoomOutAction, &QAction::triggered, this, &func_image::zoomOut);
connect(rotateLeftAction, &QAction::triggered, this, &func_image::rotateLeft);
connect(rotateRightAction, &QAction::triggered, this, &func_image::rotateRight);
// 左右图片切换按钮
QPushButton *prevButton = new QPushButton("<", this);
QPushButton *nextButton = new QPushButton(">", this);
prevButton->setFixedSize(50, 50);
nextButton->setFixedSize(50, 50);
connect(prevButton, &QPushButton::clicked, this, &func_image::showPreviousImage);
connect(nextButton, &QPushButton::clicked, this, &func_image::showNextImage);
// 状态栏用于显示当前文件名和图片序号
QStatusBar *statusBar = new QStatusBar(this);
statusBar->setSizeGripEnabled(false); // 禁止调整窗口大小
QLabel *statusLabel = new QLabel(this);
statusBar->addWidget(statusLabel);
// 布局
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QHBoxLayout *imageLayout = new QHBoxLayout();
QHBoxLayout *bottomLayout = new QHBoxLayout();
// 添加工具栏到主布局
mainLayout->addWidget(toolBar);
// 设置图片显示区布局
imageLayout->addWidget(prevButton);
imageLayout->addWidget(imageLabel);
imageLayout->addWidget(nextButton);
mainLayout->addLayout(imageLayout);
// 缩略图列表在底部
bottomLayout->addWidget(listWidget);
mainLayout->addLayout(bottomLayout);
// 添加状态栏
mainLayout->addWidget(statusBar);
setLayout(mainLayout);
// 加载图片
loadImages();
// 连接列表点击信号
connect(listWidget, &QListWidget::itemClicked, this, &func_image::showImage);
// 设置初始状态栏文本
statusLabel->setText("当前文件名: 无");
}
func_image::~func_image()
{
delete ui;
}
void func_image::loadImages()
{
QDir directory("/home/user/桌面/test"); // 替换为实际的图像文件夹路径
QStringList images = directory.entryList(QStringList() << "*.jpg" << "*.png", QDir::Files);
foreach(QString filename, images) {
QListWidgetItem *item = new QListWidgetItem(QIcon(directory.path() + "/" + filename), filename);
item->setData(Qt::UserRole, directory.path() + "/" + filename); // 保存图像路径
listWidget->addItem(item);
}
}
void func_image::showImage(QListWidgetItem *item)
{
QString imagePath = item->data(Qt::UserRole).toString();
originalPixmap.load(imagePath);
scaleFactor = 1.0;
rotationAngle = 0;
imageLabel->setPixmap(originalPixmap.scaled(imageLabel->size(), Qt::KeepAspectRatio));
// 更新状态栏
QLabel *statusLabel = findChild<QStatusBar *>()->findChild<QLabel *>();
if (statusLabel) {
int currentIndex = listWidget->currentRow() + 1;
int totalImages = listWidget->count();
statusLabel->setText(QString("当前文件名: %1 第%2/%3张").arg(imagePath).arg(currentIndex).arg(totalImages));
}
}
void func_image::showNextImage()
{
int currentIndex = listWidget->currentRow();
if (currentIndex < listWidget->count() - 1) {
listWidget->setCurrentRow(currentIndex + 1);
showImage(listWidget->currentItem());
}
}
void func_image::showPreviousImage()
{
int currentIndex = listWidget->currentRow();
if (currentIndex > 0) {
listWidget->setCurrentRow(currentIndex - 1);
showImage(listWidget->currentItem());
}
}
void func_image::zoomIn()
{
// 获取当前显示图片的尺寸
QSize currentSize = imageLabel->pixmap()->size();
QSize newSize = currentSize * 1.25;
// 限制最大放大倍数为原始图片的4倍
if (newSize.width() <= originalPixmap.size().width() * 4 &&
newSize.height() <= originalPixmap.size().height() * 4) {
imageLabel->setPixmap(originalPixmap.scaled(newSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
} else {
// 如果超出限制,则使用最大允许大小
newSize = originalPixmap.size() * 4;
imageLabel->setPixmap(originalPixmap.scaled(newSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
}
void func_image::zoomOut()
{
// 获取当前显示图片的尺寸
QSize currentSize = imageLabel->pixmap()->size();
QSize newSize = currentSize * 0.8;
imageLabel->setPixmap(originalPixmap.scaled(newSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
void func_image::rotateLeft()
{
rotationAngle -= 90;
QTransform transform;
transform.rotate(rotationAngle);
QPixmap rotatedPixmap = originalPixmap.transformed(transform, Qt::SmoothTransformation);
imageLabel->setPixmap(rotatedPixmap.scaled(imageLabel->size(), Qt::KeepAspectRatio));
}
void func_image::rotateRight()
{
rotationAngle += 90;
QTransform transform;
transform.rotate(rotationAngle);
QPixmap rotatedPixmap = originalPixmap.transformed(transform, Qt::SmoothTransformation);
imageLabel->setPixmap(rotatedPixmap.scaled(imageLabel->size(), Qt::KeepAspectRatio));
}
void func_image::wheelEvent(QWheelEvent *event)
{
if (event->angleDelta().y() > 0) {
// 向上滚动,放大图片
zoomIn();
} else {
// 向下滚动,缩小图片
zoomOut();
}
}