mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
File save dialogs
This commit is contained in:
parent
0a0a760290
commit
b9dcf3f114
@ -25,7 +25,10 @@ int show_dialog(HWND parent, bool save_dialog, LPWSTR title) {
|
|||||||
hr = CoInitialize(NULL);
|
hr = CoInitialize(NULL);
|
||||||
if (FAILED(hr)) { PRINTERR("Failed to initialize COM"); return 1; }
|
if (FAILED(hr)) { PRINTERR("Failed to initialize COM"); return 1; }
|
||||||
|
|
||||||
CALLCOM(CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, (save_dialog ? IID_IFileSaveDialog : IID_IFileOpenDialog), reinterpret_cast<LPVOID*>(&pfd)), "Failed to create COM object for file dialog")
|
CALLCOM(CoCreateInstance((save_dialog ? CLSID_FileSaveDialog : CLSID_FileOpenDialog),
|
||||||
|
NULL, CLSCTX_INPROC_SERVER, (save_dialog ? IID_IFileSaveDialog : IID_IFileOpenDialog),
|
||||||
|
reinterpret_cast<LPVOID*>(&pfd)),
|
||||||
|
"Failed to create COM object for file dialog")
|
||||||
CALLCOM(pfd->GetOptions(&dwFlags), "Failed to get options")
|
CALLCOM(pfd->GetOptions(&dwFlags), "Failed to get options")
|
||||||
dwFlags |= FOS_FORCEFILESYSTEM;
|
dwFlags |= FOS_FORCEFILESYSTEM;
|
||||||
CALLCOM(pfd->SetOptions(dwFlags), "Failed to set options")
|
CALLCOM(pfd->SetOptions(dwFlags), "Failed to set options")
|
||||||
@ -107,6 +110,8 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
|
|||||||
|
|
||||||
else if CHECK_KEY("TITLE") { READSTR(title) }
|
else if CHECK_KEY("TITLE") { READSTR(title) }
|
||||||
|
|
||||||
|
else if CHECK_KEY("SAVE_AS") { READ(1, buf); save_dialog = !!buf[0]; }
|
||||||
|
|
||||||
else {
|
else {
|
||||||
PRINTERR("Unknown key");
|
PRINTERR("Unknown key");
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -26,6 +26,10 @@ def serialize_hwnd(hwnd):
|
|||||||
return b''
|
return b''
|
||||||
return struct.pack(b'=' + (b'B4sQ' if is64bit else b'I'), 4, b'HWND', int(hwnd))
|
return struct.pack(b'=' + (b'B4sQ' if is64bit else b'I'), 4, b'HWND', int(hwnd))
|
||||||
|
|
||||||
|
def serialize_binary(key, val):
|
||||||
|
key = key.encode('ascii') if not isinstance(key, bytes) else key
|
||||||
|
return struct.pack(b'=B%ssB' % len(key), len(key), key, int(val))
|
||||||
|
|
||||||
def serialize_string(key, val):
|
def serialize_string(key, val):
|
||||||
key = key.encode('ascii') if not isinstance(key, bytes) else key
|
key = key.encode('ascii') if not isinstance(key, bytes) else key
|
||||||
val = type('')(val).encode('utf-8')
|
val = type('')(val).encode('utf-8')
|
||||||
@ -57,12 +61,14 @@ class Loop(QEventLoop):
|
|||||||
QEventLoop.__init__(self)
|
QEventLoop.__init__(self)
|
||||||
self.dialog_closed.connect(self.exit, type=Qt.QueuedConnection)
|
self.dialog_closed.connect(self.exit, type=Qt.QueuedConnection)
|
||||||
|
|
||||||
def run_file_dialog(parent=None, title=None):
|
def run_file_dialog(parent=None, title=None, save_as=False):
|
||||||
data = []
|
data = []
|
||||||
if parent is not None:
|
if parent is not None:
|
||||||
data.append(serialize_hwnd(get_hwnd(parent)))
|
data.append(serialize_hwnd(get_hwnd(parent)))
|
||||||
if title is not None:
|
if title is not None:
|
||||||
data.append(serialize_string('TITLE', title))
|
data.append(serialize_string('TITLE', title))
|
||||||
|
if save_as:
|
||||||
|
data.append(serialize_binary('SAVE_AS', save_as))
|
||||||
loop = Loop()
|
loop = Loop()
|
||||||
h = Helper(subprocess.Popen(
|
h = Helper(subprocess.Popen(
|
||||||
[HELPER], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE),
|
[HELPER], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE),
|
||||||
@ -81,7 +87,7 @@ if __name__ == '__main__':
|
|||||||
q = QMainWindow()
|
q = QMainWindow()
|
||||||
|
|
||||||
def clicked():
|
def clicked():
|
||||||
print(run_file_dialog(b, 'Testing dialogs')), sys.stdout.flush()
|
print(run_file_dialog(b, 'Testing dialogs', True)), sys.stdout.flush()
|
||||||
|
|
||||||
b = QPushButton('click me')
|
b = QPushButton('click me')
|
||||||
b.clicked.connect(clicked)
|
b.clicked.connect(clicked)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user