xref: /haiku/src/add-ons/translators/webp/ConfigView.cpp (revision 2710b4f5d4251c5cf88c82b0114ea99b0ef46d22)
167a22493SPhilippe Houdoin /*
2*c64b6fc9SPhilippe Houdoin  * Copyright 2010-2017, Haiku, Inc. All rights reserved.
367a22493SPhilippe Houdoin  * Distributed under the terms of the MIT License.
467a22493SPhilippe Houdoin  *
567a22493SPhilippe Houdoin  * Authors:
667a22493SPhilippe Houdoin  *		Philippe Houdoin
767a22493SPhilippe Houdoin  */
867a22493SPhilippe Houdoin 
967a22493SPhilippe Houdoin 
1067a22493SPhilippe Houdoin #include "ConfigView.h"
1167a22493SPhilippe Houdoin 
1267a22493SPhilippe Houdoin #include <stdio.h>
1367a22493SPhilippe Houdoin #include <string.h>
1467a22493SPhilippe Houdoin 
159f76097fSPhilippe Houdoin #include <Catalog.h>
169f76097fSPhilippe Houdoin #include <CheckBox.h>
177d48219bSHannah Boneß #include <LayoutBuilder.h>
189f76097fSPhilippe Houdoin #include <MenuField.h>
199f76097fSPhilippe Houdoin #include <MenuItem.h>
209f76097fSPhilippe Houdoin #include <Message.h>
219f76097fSPhilippe Houdoin #include <PopUpMenu.h>
229f76097fSPhilippe Houdoin #include <Slider.h>
239f76097fSPhilippe Houdoin #include <StringView.h>
249f76097fSPhilippe Houdoin 
259f76097fSPhilippe Houdoin #include "webp/encode.h"
269f76097fSPhilippe Houdoin 
279f76097fSPhilippe Houdoin #include "TranslatorSettings.h"
289f76097fSPhilippe Houdoin #include "WebPTranslator.h"
299f76097fSPhilippe Houdoin 
309f76097fSPhilippe Houdoin 
31546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT
32546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "ConfigView"
336537cf97SJérôme Duval 
3410dfe897SAxel Dörfler 
359f76097fSPhilippe Houdoin static const uint32 kMsgQuality	= 'qlty';
369f76097fSPhilippe Houdoin static const uint32 kMsgPreset	= 'prst';
379f76097fSPhilippe Houdoin static const uint32 kMsgMethod	= 'metd';
389f76097fSPhilippe Houdoin static const uint32 kMsgPreprocessing = 'pprc';
3967a22493SPhilippe Houdoin 
409f76097fSPhilippe Houdoin static const struct preset_name {
419f76097fSPhilippe Houdoin 	const char*	name;
429f76097fSPhilippe Houdoin 	WebPPreset	id;
439f76097fSPhilippe Houdoin } kPresetNames[] = {
449f76097fSPhilippe Houdoin 	{ B_TRANSLATE("Default"), 	WEBP_PRESET_DEFAULT },
459f76097fSPhilippe Houdoin 	{ B_TRANSLATE("Picture"), 	WEBP_PRESET_PICTURE },
469f76097fSPhilippe Houdoin 	{ B_TRANSLATE("Photo"), 	WEBP_PRESET_PHOTO },
479f76097fSPhilippe Houdoin 	{ B_TRANSLATE("Drawing"), 	WEBP_PRESET_DRAWING },
489f76097fSPhilippe Houdoin 	{ B_TRANSLATE("Icon"), 		WEBP_PRESET_ICON },
499f76097fSPhilippe Houdoin 	{ B_TRANSLATE("Text"), 		WEBP_PRESET_TEXT },
509f76097fSPhilippe Houdoin 	{ NULL },
519f76097fSPhilippe Houdoin };
529f76097fSPhilippe Houdoin 
539f76097fSPhilippe Houdoin 
ConfigView(TranslatorSettings * settings)546195bee5SAdrien Destugues ConfigView::ConfigView(TranslatorSettings* settings)
55abfb31b6SJohn Scipione 	:
56abfb31b6SJohn Scipione 	BGroupView(B_TRANSLATE("WebPTranslator Settings"), B_VERTICAL),
579f76097fSPhilippe Houdoin 	fSettings(settings)
5867a22493SPhilippe Houdoin {
59f0650dc9Slooncraz 	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
6067a22493SPhilippe Houdoin 
61468f8266SHumdinger 	BStringView* title = new BStringView("title",
62468f8266SHumdinger 		B_TRANSLATE("WebP image translator"));
6395d32039SPhilippe Houdoin 	title->SetFont(be_bold_font);
6467a22493SPhilippe Houdoin 
6595d32039SPhilippe Houdoin 	char versionString[256];
66037bc75eSJanus 	sprintf(versionString, "v%d.%d.%d, %s",
676537cf97SJérôme Duval 		static_cast<int>(B_TRANSLATION_MAJOR_VERSION(WEBP_TRANSLATOR_VERSION)),
686537cf97SJérôme Duval 		static_cast<int>(B_TRANSLATION_MINOR_VERSION(WEBP_TRANSLATOR_VERSION)),
696537cf97SJérôme Duval 		static_cast<int>(B_TRANSLATION_REVISION_VERSION(
70037bc75eSJanus 			WEBP_TRANSLATOR_VERSION)),
71037bc75eSJanus 		__DATE__);
729f76097fSPhilippe Houdoin 
7395d32039SPhilippe Houdoin 	BStringView* version = new BStringView("version", versionString);
7467a22493SPhilippe Houdoin 
759f76097fSPhilippe Houdoin 	BString copyrightsText;
76037bc75eSJanus 	BStringView *copyrightView = new BStringView("Copyright",
77*c64b6fc9SPhilippe Houdoin 		B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2017 Haiku Inc."));
78*c64b6fc9SPhilippe Houdoin 
79*c64b6fc9SPhilippe Houdoin 	BString libwebpInfo = B_TRANSLATE(
80*c64b6fc9SPhilippe Houdoin 		"Based on libwebp %version%");
81*c64b6fc9SPhilippe Houdoin 	int v = WebPGetEncoderVersion();
82*c64b6fc9SPhilippe Houdoin 	char libwebpVersion[32];
83*c64b6fc9SPhilippe Houdoin 	snprintf(libwebpVersion, sizeof(libwebpVersion),
84*c64b6fc9SPhilippe Houdoin 		"%d.%d.%d", v >> 16, (v>>8)&255, v&255);
85*c64b6fc9SPhilippe Houdoin 	libwebpInfo.ReplaceAll("%version%", libwebpVersion);
86*c64b6fc9SPhilippe Houdoin 
87037bc75eSJanus 	BStringView *copyright2View = new BStringView("Copyright2",
88*c64b6fc9SPhilippe Houdoin 		libwebpInfo.String());
89037bc75eSJanus 	BStringView *copyright3View = new BStringView("Copyright3",
90*c64b6fc9SPhilippe Houdoin 		B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2017 Google Inc."));
9167a22493SPhilippe Houdoin 
929f76097fSPhilippe Houdoin 	// output parameters
939f76097fSPhilippe Houdoin 
949f76097fSPhilippe Houdoin 	fPresetsMenu = new BPopUpMenu(B_TRANSLATE("Preset"));
959f76097fSPhilippe Houdoin 	const struct preset_name* preset = kPresetNames;
969f76097fSPhilippe Houdoin 	while (preset->name != NULL) {
979f76097fSPhilippe Houdoin 		BMessage* msg = new BMessage(kMsgPreset);
989f76097fSPhilippe Houdoin 		msg->AddInt32("value", preset->id);
999f76097fSPhilippe Houdoin 
1009f76097fSPhilippe Houdoin 		BMenuItem* item = new BMenuItem(preset->name, msg);
1019f76097fSPhilippe Houdoin 		if (fSettings->SetGetInt32(WEBP_SETTING_PRESET) == preset->id)
1029f76097fSPhilippe Houdoin 			item->SetMarked(true);
1039f76097fSPhilippe Houdoin 		fPresetsMenu->AddItem(item);
1049f76097fSPhilippe Houdoin 
1059f76097fSPhilippe Houdoin 		preset++;
1069f76097fSPhilippe Houdoin 	}
1079f76097fSPhilippe Houdoin 	BMenuField* presetsField = new BMenuField(B_TRANSLATE("Output preset:"),
10810dfe897SAxel Dörfler 		fPresetsMenu);
1099f76097fSPhilippe Houdoin 
1109f76097fSPhilippe Houdoin 	fQualitySlider = new BSlider("quality", B_TRANSLATE("Output quality:"),
1119f76097fSPhilippe Houdoin 		new BMessage(kMsgQuality), 0, 100, B_HORIZONTAL, B_BLOCK_THUMB);
1129f76097fSPhilippe Houdoin 	fQualitySlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
1139f76097fSPhilippe Houdoin 	fQualitySlider->SetHashMarkCount(10);
1149f76097fSPhilippe Houdoin 	fQualitySlider->SetLimitLabels(B_TRANSLATE("Low"), B_TRANSLATE("High"));
1159f76097fSPhilippe Houdoin 	fQualitySlider->SetValue(fSettings->SetGetInt32(WEBP_SETTING_QUALITY));
1169f76097fSPhilippe Houdoin 
1179f76097fSPhilippe Houdoin 	fMethodSlider = new BSlider("method", B_TRANSLATE("Compression method:"),
1189f76097fSPhilippe Houdoin 		new BMessage(kMsgMethod), 0, 6, B_HORIZONTAL, B_BLOCK_THUMB);
1199f76097fSPhilippe Houdoin 	fMethodSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
1209f76097fSPhilippe Houdoin 	fMethodSlider->SetHashMarkCount(7);
1219f76097fSPhilippe Houdoin 	fMethodSlider->SetLimitLabels(B_TRANSLATE("Fast"),
1229f76097fSPhilippe Houdoin 		B_TRANSLATE("Slower but better"));
1239f76097fSPhilippe Houdoin 	fMethodSlider->SetValue(fSettings->SetGetInt32(WEBP_SETTING_METHOD));
1249f76097fSPhilippe Houdoin 
1259f76097fSPhilippe Houdoin 	fPreprocessingCheckBox = new BCheckBox("preprocessing",
1269f76097fSPhilippe Houdoin 		B_TRANSLATE("Preprocessing filter"), new BMessage(kMsgPreprocessing));
1279f76097fSPhilippe Houdoin 	if (fSettings->SetGetBool(WEBP_SETTING_PREPROCESSING))
1289f76097fSPhilippe Houdoin 		fPreprocessingCheckBox->SetValue(B_CONTROL_ON);
12967a22493SPhilippe Houdoin 
13067a22493SPhilippe Houdoin 	// Build the layout
131037bc75eSJanus 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
132037bc75eSJanus 		.SetInsets(B_USE_DEFAULT_SPACING)
13395d32039SPhilippe Houdoin 		.Add(title)
13495d32039SPhilippe Houdoin 		.Add(version)
135037bc75eSJanus 		.Add(copyrightView)
13667a22493SPhilippe Houdoin 		.AddGlue()
137e465dcb6SJohn Scipione 		.AddGrid(B_USE_DEFAULT_SPACING, B_USE_SMALL_SPACING)
138e465dcb6SJohn Scipione 			.Add(presetsField->CreateLabelLayoutItem(), 0, 0)
139e465dcb6SJohn Scipione 			.AddGroup(B_HORIZONTAL, 0.0f, 1, 0)
140e465dcb6SJohn Scipione 				.Add(presetsField->CreateMenuBarLayoutItem(), 0.0f)
141e465dcb6SJohn Scipione 				.AddGlue()
142e465dcb6SJohn Scipione 				.End()
143e465dcb6SJohn Scipione 			.End()
1449f76097fSPhilippe Houdoin 		.Add(fQualitySlider)
1459f76097fSPhilippe Houdoin 		.Add(fMethodSlider)
146037bc75eSJanus 		.Add(fPreprocessingCheckBox)
147037bc75eSJanus 		.AddGlue()
148037bc75eSJanus 		.Add(copyright2View)
149037bc75eSJanus 		.Add(copyright3View);
15067a22493SPhilippe Houdoin }
15167a22493SPhilippe Houdoin 
15267a22493SPhilippe Houdoin 
~ConfigView()15367a22493SPhilippe Houdoin ConfigView::~ConfigView()
15467a22493SPhilippe Houdoin {
1559f76097fSPhilippe Houdoin 	fSettings->Release();
15667a22493SPhilippe Houdoin }
15767a22493SPhilippe Houdoin 
1589f76097fSPhilippe Houdoin 
1599f76097fSPhilippe Houdoin void
AttachedToWindow()1609f76097fSPhilippe Houdoin ConfigView::AttachedToWindow()
1619f76097fSPhilippe Houdoin {
1626195bee5SAdrien Destugues 	BGroupView::AttachedToWindow();
1639f76097fSPhilippe Houdoin 
1649f76097fSPhilippe Houdoin 	fPresetsMenu->SetTargetForItems(this);
1659f76097fSPhilippe Houdoin 
1669f76097fSPhilippe Houdoin 	fQualitySlider->SetTarget(this);
1679f76097fSPhilippe Houdoin 	fMethodSlider->SetTarget(this);
1689f76097fSPhilippe Houdoin 	fPreprocessingCheckBox->SetTarget(this);
1696195bee5SAdrien Destugues 
1706195bee5SAdrien Destugues 	if (Parent() == NULL && Window()->GetLayout() == NULL) {
1716195bee5SAdrien Destugues 		Window()->SetLayout(new BGroupLayout(B_VERTICAL));
1726195bee5SAdrien Destugues 		Window()->ResizeTo(PreferredSize().Width(), PreferredSize().Height());
1736195bee5SAdrien Destugues 	}
1749f76097fSPhilippe Houdoin }
1759f76097fSPhilippe Houdoin 
1769f76097fSPhilippe Houdoin 
1779f76097fSPhilippe Houdoin void
MessageReceived(BMessage * message)1789f76097fSPhilippe Houdoin ConfigView::MessageReceived(BMessage* message)
1799f76097fSPhilippe Houdoin {
1809f76097fSPhilippe Houdoin 	struct {
1819f76097fSPhilippe Houdoin 		const char*		name;
1829f76097fSPhilippe Houdoin 		uint32			what;
1839f76097fSPhilippe Houdoin 		TranSettingType	type;
1849f76097fSPhilippe Houdoin 	} maps[] = {
1859f76097fSPhilippe Houdoin 		{ WEBP_SETTING_PRESET, kMsgPreset, TRAN_SETTING_INT32 },
1869f76097fSPhilippe Houdoin 		{ WEBP_SETTING_QUALITY, kMsgQuality, TRAN_SETTING_INT32 },
1879f76097fSPhilippe Houdoin 		{ WEBP_SETTING_METHOD, kMsgMethod, TRAN_SETTING_INT32 },
1889f76097fSPhilippe Houdoin 		{ WEBP_SETTING_PREPROCESSING, kMsgPreprocessing, TRAN_SETTING_BOOL },
1899f76097fSPhilippe Houdoin 		{ NULL }
1909f76097fSPhilippe Houdoin 	};
1919f76097fSPhilippe Houdoin 
1929f76097fSPhilippe Houdoin 	int i;
1939f76097fSPhilippe Houdoin 	for (i = 0; maps[i].name != NULL; i++) {
1949f76097fSPhilippe Houdoin 		if (maps[i].what == message->what)
1959f76097fSPhilippe Houdoin 			break;
1969f76097fSPhilippe Houdoin 	}
1979f76097fSPhilippe Houdoin 
1989f76097fSPhilippe Houdoin 	if (maps[i].name == NULL) {
1996195bee5SAdrien Destugues 		BGroupView::MessageReceived(message);
2009f76097fSPhilippe Houdoin 		return;
2019f76097fSPhilippe Houdoin 	}
2029f76097fSPhilippe Houdoin 
2039f76097fSPhilippe Houdoin 	int32 value;
2049f76097fSPhilippe Houdoin 	if (message->FindInt32("value", &value) == B_OK
2059f76097fSPhilippe Houdoin 		|| message->FindInt32("be:value", &value) == B_OK) {
2069f76097fSPhilippe Houdoin 		switch(maps[i].type) {
2079f76097fSPhilippe Houdoin 			case TRAN_SETTING_BOOL:
2089f76097fSPhilippe Houdoin 			{
2099f76097fSPhilippe Houdoin 				bool boolValue = value;
2109f76097fSPhilippe Houdoin 				fSettings->SetGetBool(maps[i].name, &boolValue);
2119f76097fSPhilippe Houdoin 				break;
2129f76097fSPhilippe Houdoin 			}
2139f76097fSPhilippe Houdoin 			case TRAN_SETTING_INT32:
2149f76097fSPhilippe Houdoin 				fSettings->SetGetInt32(maps[i].name, &value);
2159f76097fSPhilippe Houdoin 				break;
2169f76097fSPhilippe Houdoin 		}
2179f76097fSPhilippe Houdoin 		fSettings->SaveSettings();
2189f76097fSPhilippe Houdoin 	}
2199f76097fSPhilippe Houdoin }
220