Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
柴进
/
GoogleNanoBananaApp
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
3689be68
authored
2025-12-10 17:53:33 +0800
by
柴进
1
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
增加队列模式,支持同时做多个任务
1 parent
ae44a010
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
153 additions
and
37 deletions
image_generator.py
image_generator.py
View file @
3689be6
...
...
@@ -1043,6 +1043,10 @@ class ImageGeneratorWindow(QMainWindow):
# Initialize history manager
self
.
history_manager
=
HistoryManager
()
# Initialize task queue manager
from
task_queue
import
TaskQueueManager
self
.
task_manager
=
TaskQueueManager
()
self
.
setup_ui
()
self
.
apply_styles
()
...
...
@@ -1254,10 +1258,14 @@ class ImageGeneratorWindow(QMainWindow):
central_widget
=
QWidget
()
self
.
setCentralWidget
(
central_widget
)
main_layout
=
Q
V
BoxLayout
()
main_layout
=
Q
H
BoxLayout
()
main_layout
.
setContentsMargins
(
10
,
10
,
10
,
10
)
main_layout
.
setSpacing
(
10
)
# Left side: Main content
content_layout
=
QVBoxLayout
()
content_layout
.
setSpacing
(
10
)
# Create tab widget
self
.
tab_widget
=
QTabWidget
()
...
...
@@ -1279,7 +1287,20 @@ class ImageGeneratorWindow(QMainWindow):
history_tab
=
self
.
setup_history_tab
()
self
.
tab_widget
.
addTab
(
history_tab
,
"历史记录"
)
main_layout
.
addWidget
(
self
.
tab_widget
)
content_layout
.
addWidget
(
self
.
tab_widget
)
# Create content widget with vertical layout
content_widget
=
QWidget
()
content_widget
.
setLayout
(
content_layout
)
# Add content to left side
main_layout
.
addWidget
(
content_widget
,
7
)
# 70% width
# Right side: Task queue sidebar
from
task_queue
import
TaskQueueWidget
self
.
task_queue_widget
=
TaskQueueWidget
(
self
.
task_manager
)
main_layout
.
addWidget
(
self
.
task_queue_widget
,
3
)
# 30% width
central_widget
.
setLayout
(
main_layout
)
self
.
check_favorite_status
()
...
...
@@ -1923,26 +1944,66 @@ class ImageGeneratorWindow(QMainWindow):
self
.
status_label
.
setStyleSheet
(
"QLabel { color: #34C759; }"
)
def
generate_image_async
(
self
):
"""Start image generation in a separate thread"""
# Create and start worker thread
self
.
worker
=
ImageGenerationWorker
(
self
.
api_key
,
self
.
prompt_text
.
toPlainText
()
.
strip
(),
self
.
uploaded_images
,
self
.
aspect_ratio
.
currentText
(),
self
.
image_size
.
currentText
(),
"gemini-3-pro-image-preview"
# 锁死模型
)
self
.
worker
.
finished
.
connect
(
self
.
on_image_generated
)
self
.
worker
.
error
.
connect
(
self
.
on_generation_error
)
self
.
worker
.
progress
.
connect
(
self
.
update_status
)
"""Submit image generation task to queue"""
from
task_queue
import
TaskType
self
.
generate_btn
.
setEnabled
(
False
)
self
.
download_btn
.
setEnabled
(
False
)
self
.
status_label
.
setText
(
"● 正在生成图片..."
)
self
.
status_label
.
setStyleSheet
(
"QLabel { color: #FF9500; }"
)
prompt
=
self
.
prompt_text
.
toPlainText
()
.
strip
()
if
not
prompt
:
QMessageBox
.
warning
(
self
,
"提示"
,
"请输入图片描述!"
)
return
try
:
# Submit task to queue
task_id
=
self
.
task_manager
.
submit_task
(
task_type
=
TaskType
.
IMAGE_GENERATION
,
prompt
=
prompt
,
api_key
=
self
.
api_key
,
reference_images
=
self
.
uploaded_images
.
copy
(),
aspect_ratio
=
self
.
aspect_ratio
.
currentText
(),
image_size
=
self
.
image_size
.
currentText
(),
model
=
"gemini-3-pro-image-preview"
)
# Connect to task completion signal
self
.
task_manager
.
task_completed
.
connect
(
lambda
tid
,
img
,
p
,
refs
,
ar
,
size
,
model
:
self
.
_on_my_task_completed
(
task_id
,
tid
,
img
,
p
,
refs
,
ar
,
size
,
model
)
)
self
.
worker
.
start
()
# Update UI
self
.
status_label
.
setText
(
f
"● 任务已提交 (ID: {task_id[:8]})"
)
self
.
status_label
.
setStyleSheet
(
"QLabel { color: #007AFF; }"
)
except
RuntimeError
as
e
:
QMessageBox
.
warning
(
self
,
"队列已满"
,
str
(
e
))
def
_on_my_task_completed
(
self
,
my_task_id
,
task_id
,
image_bytes
,
prompt
,
reference_images
,
aspect_ratio
,
image_size
,
model
):
"""Handle completion of my submitted task"""
if
my_task_id
!=
task_id
:
return
# Not my task
# Update display
self
.
generated_image_bytes
=
image_bytes
self
.
display_image
()
self
.
download_btn
.
setEnabled
(
True
)
self
.
status_label
.
setText
(
"● 图片生成成功"
)
self
.
status_label
.
setStyleSheet
(
"QLabel { color: #34C759; }"
)
# Save to history
try
:
self
.
history_manager
.
save_generation
(
image_bytes
=
image_bytes
,
prompt
=
prompt
,
reference_images
=
reference_images
,
aspect_ratio
=
aspect_ratio
,
image_size
=
image_size
,
model
=
model
)
self
.
status_label
.
setText
(
"● 图片生成成功,已保存到历史记录"
)
self
.
refresh_history
()
except
Exception
as
e
:
self
.
logger
.
warning
(
f
"保存到历史记录失败: {e}"
)
def
on_image_generated
(
self
,
image_bytes
,
prompt
,
reference_images
,
aspect_ratio
,
image_size
,
model
):
"""Handle successful image generation"""
...
...
@@ -3239,7 +3300,9 @@ class StyleDesignerTab(QWidget):
combo
.
setCurrentIndex
(
0
)
def
generate_image
(
self
):
"""调用文生图 API"""
"""Submit image generation task to queue"""
from
task_queue
import
TaskType
# 获取 prompt
prompt
=
self
.
prompt_preview
.
toPlainText
()
if
not
prompt
.
strip
():
...
...
@@ -3249,36 +3312,61 @@ class StyleDesignerTab(QWidget):
# 获取设置
aspect_ratio
=
self
.
aspect_ratio
.
currentText
()
image_size
=
self
.
image_size
.
currentText
()
model
=
"gemini-3-pro-image-preview"
# 硬编码使用默认模型
model
=
"gemini-3-pro-image-preview"
# 获取父窗口的 API key
if
not
hasattr
(
self
.
parent_window
,
'api_key'
)
or
not
self
.
parent_window
.
api_key
:
QMessageBox
.
warning
(
self
,
"错误"
,
"未找到 API 密钥,请在主窗口配置"
)
return
# 非阻塞状态提示
self
.
generate_btn
.
setEnabled
(
False
)
self
.
status_label
.
setText
(
"● 生成中..."
)
QApplication
.
processEvents
()
try
:
#
创建生成线程
self
.
gen_thread
=
ImageGenerationWorker
(
api_key
=
self
.
parent_window
.
api_key
,
#
Submit task to queue
task_id
=
self
.
parent_window
.
task_manager
.
submit_task
(
task_type
=
TaskType
.
STYLE_DESIGN
,
prompt
=
prompt
,
images
=
[],
api_key
=
self
.
parent_window
.
api_key
,
reference_images
=
[],
aspect_ratio
=
aspect_ratio
,
image_size
=
image_size
,
model
=
model
)
self
.
gen_thread
.
finished
.
connect
(
self
.
on_generation_success
)
self
.
gen_thread
.
error
.
connect
(
self
.
on_generation_error
)
self
.
gen_thread
.
start
()
# Connect to task completion signal
self
.
parent_window
.
task_manager
.
task_completed
.
connect
(
lambda
tid
,
img
,
p
,
refs
,
ar
,
size
,
mdl
:
self
.
_on_my_task_completed
(
task_id
,
tid
,
img
,
p
,
refs
,
ar
,
size
,
mdl
)
)
# Update UI
self
.
status_label
.
setText
(
f
"● 任务已提交 (ID: {task_id[:8]})"
)
except
RuntimeError
as
e
:
QMessageBox
.
warning
(
self
,
"队列已满"
,
str
(
e
))
def
_on_my_task_completed
(
self
,
my_task_id
,
task_id
,
image_bytes
,
prompt
,
reference_images
,
aspect_ratio
,
image_size
,
model
):
"""Handle completion of my submitted task"""
if
my_task_id
!=
task_id
:
return
# Not my task
# Update display
self
.
generated_image_bytes
=
image_bytes
self
.
_display_generated_image_from_bytes
()
self
.
status_label
.
setText
(
"● 生成成功"
)
# Save to history
try
:
self
.
parent_window
.
history_manager
.
save_generation
(
image_bytes
=
image_bytes
,
prompt
=
prompt
,
reference_images
=
reference_images
,
aspect_ratio
=
aspect_ratio
,
image_size
=
image_size
,
model
=
model
)
self
.
parent_window
.
refresh_history
()
except
Exception
as
e
:
self
.
generate_btn
.
setEnabled
(
True
)
self
.
status_label
.
setText
(
"● 就绪"
)
QMessageBox
.
critical
(
self
,
"错误"
,
f
"生成失败: {e}"
)
self
.
logger
.
warning
(
f
"保存到历史记录失败: {e}"
)
def
on_generation_success
(
self
,
image_bytes
:
bytes
):
"""生成成功回调"""
...
...
@@ -3349,6 +3437,34 @@ class StyleDesignerTab(QWidget):
self
.
status_label
.
setText
(
"● 就绪"
)
QMessageBox
.
critical
(
self
,
"生成失败"
,
error_msg
)
def
_display_generated_image_from_bytes
(
self
):
"""从字节数据显示生成的图片"""
if
not
self
.
generated_image_bytes
:
return
try
:
pixmap
=
QPixmap
()
pixmap
.
loadFromData
(
self
.
generated_image_bytes
)
if
not
pixmap
.
isNull
():
# 缩放以适应标签大小
scaled_pixmap
=
pixmap
.
scaled
(
self
.
result_label
.
size
(),
Qt
.
KeepAspectRatio
,
Qt
.
SmoothTransformation
)
self
.
result_label
.
setPixmap
(
scaled_pixmap
)
self
.
result_label
.
setStyleSheet
(
"""
QLabel {
border: 1px solid #ddd;
background-color: white;
}
"""
)
# 启用下载按钮
self
.
download_btn
.
setEnabled
(
True
)
except
Exception
as
e
:
self
.
logger
.
error
(
f
"显示图片失败: {e}"
)
def
download_image
(
self
):
"""下载图片"""
if
not
self
.
generated_image_bytes
:
...
...
柴进
@chaijin
mentioned in commit
f4ceb240
2025-12-12 16:48:56 +0800
mentioned in commit
f4ceb240
Toggle commit list
Please
register
or
sign in
to post a comment