All pastes #2089711 Raw Edit

Miscellany

public cpp v1 · immutable
#2089711 ·published 2011-10-13 12:49 UTC
rendered paste body
QString PlatformUtils::getSaveFileName(const QString &title, const QString &filter, const QString &currentDir, const QString &currentFile){	QString f;	QRegExp rx("([a-zA-Z0-9 -]*)\\(([a-zA-Z0-9_.*? +;#\\-\\[\\]@\\{\\}/!<>\\$%&=^~:\\|]*)\\)$");	int i = rx.indexIn(filter);	QStringList rxlist = rx.capturedTexts();	if (i > -1)		f = rx.cap(2);	QStringList list = f.split(QLatin1Char(' '), QString::SkipEmptyParts);#ifdef _WIN32	QString tFilters = filter + QChar();	for (int i = 0; i < list.size(); ++i)		tFilters += list[i] + ";";	tFilters += QChar();	QString tTitle = title;	QString tInitDir = QDir::toNativeSeparators(currentDir);	QString initSel = QDir::toNativeSeparators(currentFile);	if (!initSel.isEmpty()) 	{		initSel.remove(QLatin1Char('<'));		initSel.remove(QLatin1Char('>'));		initSel.remove(QLatin1Char('\"'));		initSel.remove(QLatin1Char('|'));	}	wchar_t *tInitSel = new wchar_t[256 + 1];	if (initSel.length() > 0 && initSel.length() <= 256)		memcpy(tInitSel, initSel.utf16(), (initSel.length()+1)*sizeof(QChar));	else		tInitSel[0] = 0;	OPENFILENAME ofn;	ZeroMemory(&ofn, sizeof(ofn));	ofn.lStructSize = sizeof(ofn);	ofn.hwndOwner = GetActiveWindow();	ofn.lpstrTitle = (WCHAR*)tTitle.utf16();	ofn.lpstrFile = tInitSel;	ofn.lpstrFilter = (WCHAR*)tFilters.utf16();	ofn.nMaxFile = 0x400;	ofn.nFilterIndex = 1;	ofn.lpstrFileTitle = NULL;	ofn.nMaxFileTitle = 0;	ofn.lpstrInitialDir = (wchar_t*)tInitDir.utf16();	ofn.Flags = OFN_NOCHANGEDIR | OFN_HIDEREADONLY | OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_FILEMUSTEXIST;	if (!GetSaveFileNameW(&ofn))		return QString("");	QString fn = QString::fromUtf16((ushort*)ofn.lpstrFile);	return fn;#elif __APPLE__ & __MACH__	NSSavePanel *panel = [NSSavePanel savePanel];	NSMutableArray *fileTypes = [[NSMutableArray alloc] init];	for (int i = 0; i < list.size(); i++)	{		[fileTypes addObject:[NSString stringWithCharacters:(const unichar*)list[i].mid(2).utf16() length:(NSUInteger)list[i].mid(2).length()]];		}	 	QString initDir = QDir::toNativeSeparators(currentDir);	[panel setAllowedFileTypes:fileTypes];	[panel setDirectory:[NSString stringWithCharacters:(const unichar*)initDir.utf16() length:(NSUInteger)initDir.length()]];		int res = [panel runModal];	if (res == NSFileHandlingPanelOKButton)	{	    return QString::fromUtf8(reinterpret_cast<const char*>([[panel filename] UTF8String]));	}	return "";#endif}