2 * Copyright 2010-2011, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
10 #include "ConfigView.h"
17 #include <LayoutBuilder.h>
18 #include <MenuField.h>
21 #include <PopUpMenu.h>
23 #include <StringView.h>
25 #include "webp/encode.h"
27 #include "TranslatorSettings.h"
28 #include "WebPTranslator.h"
31 #undef B_TRANSLATION_CONTEXT
32 #define B_TRANSLATION_CONTEXT "ConfigView"
35 static const uint32 kMsgQuality
= 'qlty';
36 static const uint32 kMsgPreset
= 'prst';
37 static const uint32 kMsgMethod
= 'metd';
38 static const uint32 kMsgPreprocessing
= 'pprc';
40 static const struct preset_name
{
44 { B_TRANSLATE("Default"), WEBP_PRESET_DEFAULT
},
45 { B_TRANSLATE("Picture"), WEBP_PRESET_PICTURE
},
46 { B_TRANSLATE("Photo"), WEBP_PRESET_PHOTO
},
47 { B_TRANSLATE("Drawing"), WEBP_PRESET_DRAWING
},
48 { B_TRANSLATE("Icon"), WEBP_PRESET_ICON
},
49 { B_TRANSLATE("Text"), WEBP_PRESET_TEXT
},
54 ConfigView::ConfigView(TranslatorSettings
* settings
)
55 : BGroupView(B_TRANSLATE("WebPTranslator Settings"), B_VERTICAL
),
58 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR
));
60 BStringView
* title
= new BStringView("title",
61 B_TRANSLATE("WebP image translator"));
62 title
->SetFont(be_bold_font
);
64 char versionString
[256];
65 sprintf(versionString
, "v%d.%d.%d, %s",
66 static_cast<int>(B_TRANSLATION_MAJOR_VERSION(WEBP_TRANSLATOR_VERSION
)),
67 static_cast<int>(B_TRANSLATION_MINOR_VERSION(WEBP_TRANSLATOR_VERSION
)),
68 static_cast<int>(B_TRANSLATION_REVISION_VERSION(
69 WEBP_TRANSLATOR_VERSION
)),
72 BStringView
* version
= new BStringView("version", versionString
);
74 BString copyrightsText
;
75 BStringView
*copyrightView
= new BStringView("Copyright",
76 B_TRANSLATE(B_UTF8_COPYRIGHT
"2010-2011 Haiku Inc."));
77 BStringView
*copyright2View
= new BStringView("Copyright2",
78 B_TRANSLATE("Based on libwebp v0.1,"));
79 BStringView
*copyright3View
= new BStringView("Copyright3",
80 B_TRANSLATE(B_UTF8_COPYRIGHT
"2010-2011 Google Inc."));
84 fPresetsMenu
= new BPopUpMenu(B_TRANSLATE("Preset"));
85 const struct preset_name
* preset
= kPresetNames
;
86 while (preset
->name
!= NULL
) {
87 BMessage
* msg
= new BMessage(kMsgPreset
);
88 msg
->AddInt32("value", preset
->id
);
90 BMenuItem
* item
= new BMenuItem(preset
->name
, msg
);
91 if (fSettings
->SetGetInt32(WEBP_SETTING_PRESET
) == preset
->id
)
92 item
->SetMarked(true);
93 fPresetsMenu
->AddItem(item
);
97 BMenuField
* presetsField
= new BMenuField(B_TRANSLATE("Output preset:"),
100 fQualitySlider
= new BSlider("quality", B_TRANSLATE("Output quality:"),
101 new BMessage(kMsgQuality
), 0, 100, B_HORIZONTAL
, B_BLOCK_THUMB
);
102 fQualitySlider
->SetHashMarks(B_HASH_MARKS_BOTTOM
);
103 fQualitySlider
->SetHashMarkCount(10);
104 fQualitySlider
->SetLimitLabels(B_TRANSLATE("Low"), B_TRANSLATE("High"));
105 fQualitySlider
->SetValue(fSettings
->SetGetInt32(WEBP_SETTING_QUALITY
));
107 fMethodSlider
= new BSlider("method", B_TRANSLATE("Compression method:"),
108 new BMessage(kMsgMethod
), 0, 6, B_HORIZONTAL
, B_BLOCK_THUMB
);
109 fMethodSlider
->SetHashMarks(B_HASH_MARKS_BOTTOM
);
110 fMethodSlider
->SetHashMarkCount(7);
111 fMethodSlider
->SetLimitLabels(B_TRANSLATE("Fast"),
112 B_TRANSLATE("Slower but better"));
113 fMethodSlider
->SetValue(fSettings
->SetGetInt32(WEBP_SETTING_METHOD
));
115 fPreprocessingCheckBox
= new BCheckBox("preprocessing",
116 B_TRANSLATE("Preprocessing filter"), new BMessage(kMsgPreprocessing
));
117 if (fSettings
->SetGetBool(WEBP_SETTING_PREPROCESSING
))
118 fPreprocessingCheckBox
->SetValue(B_CONTROL_ON
);
121 BLayoutBuilder::Group
<>(this, B_VERTICAL
, 0)
122 .SetInsets(B_USE_DEFAULT_SPACING
)
130 .Add(fPreprocessingCheckBox
)
133 .Add(copyright3View
);
137 SetExplicitPreferredSize(BSize((font
.Size() * 250)/12, (font
.Size() * 350)/12));
142 ConfigView::~ConfigView()
144 fSettings
->Release();
149 ConfigView::AttachedToWindow()
151 BGroupView::AttachedToWindow();
153 fPresetsMenu
->SetTargetForItems(this);
155 fQualitySlider
->SetTarget(this);
156 fMethodSlider
->SetTarget(this);
157 fPreprocessingCheckBox
->SetTarget(this);
159 if (Parent() == NULL
&& Window()->GetLayout() == NULL
) {
160 Window()->SetLayout(new BGroupLayout(B_VERTICAL
));
161 Window()->ResizeTo(PreferredSize().Width(), PreferredSize().Height());
167 ConfigView::MessageReceived(BMessage
* message
)
172 TranSettingType type
;
174 { WEBP_SETTING_PRESET
, kMsgPreset
, TRAN_SETTING_INT32
},
175 { WEBP_SETTING_QUALITY
, kMsgQuality
, TRAN_SETTING_INT32
},
176 { WEBP_SETTING_METHOD
, kMsgMethod
, TRAN_SETTING_INT32
},
177 { WEBP_SETTING_PREPROCESSING
, kMsgPreprocessing
, TRAN_SETTING_BOOL
},
182 for (i
= 0; maps
[i
].name
!= NULL
; i
++) {
183 if (maps
[i
].what
== message
->what
)
187 if (maps
[i
].name
== NULL
) {
188 BGroupView::MessageReceived(message
);
193 if (message
->FindInt32("value", &value
) == B_OK
194 || message
->FindInt32("be:value", &value
) == B_OK
) {
195 switch(maps
[i
].type
) {
196 case TRAN_SETTING_BOOL
:
198 bool boolValue
= value
;
199 fSettings
->SetGetBool(maps
[i
].name
, &boolValue
);
202 case TRAN_SETTING_INT32
:
203 fSettings
->SetGetInt32(maps
[i
].name
, &value
);
206 fSettings
->SaveSettings();