Add commandline option to resize window

This commit is contained in:
Jonas Kvinge
2021-04-14 19:50:38 +02:00
parent 2a8490ef31
commit 4c0f7c3dd4
3 changed files with 63 additions and 9 deletions

View File

@@ -71,10 +71,11 @@ const char *CommandlineOptions::kHelpText =
" -o, --show-osd %27\n" " -o, --show-osd %27\n"
" -y, --toggle-pretty-osd %28\n" " -y, --toggle-pretty-osd %28\n"
" -g, --language <lang> %29\n" " -g, --language <lang> %29\n"
" --quiet %30\n" " -w, --resize-window <WxH> %30\n"
" --verbose %31\n" " --quiet %31\n"
" --log-levels <levels> %32\n" " --verbose %32\n"
" --version %33\n"; " --log-levels <levels> %33\n"
" --version %34\n";
const char *CommandlineOptions::kVersionText = "Strawberry %1"; const char *CommandlineOptions::kVersionText = "Strawberry %1";
@@ -143,6 +144,7 @@ bool CommandlineOptions::Parse() {
{"show-osd", no_argument, nullptr, 'o'}, {"show-osd", no_argument, nullptr, 'o'},
{"toggle-pretty-osd", no_argument, nullptr, 'y'}, {"toggle-pretty-osd", no_argument, nullptr, 'y'},
{"language", required_argument, nullptr, 'g'}, {"language", required_argument, nullptr, 'g'},
{"resize-window", required_argument, nullptr, 'w'},
{"quiet", no_argument, nullptr, Quiet}, {"quiet", no_argument, nullptr, Quiet},
{"verbose", no_argument, nullptr, Verbose}, {"verbose", no_argument, nullptr, Verbose},
{"log-levels", required_argument, nullptr, LogLevels}, {"log-levels", required_argument, nullptr, LogLevels},
@@ -152,7 +154,7 @@ bool CommandlineOptions::Parse() {
// Parse the arguments // Parse the arguments
bool ok = false; bool ok = false;
forever { forever {
int c = getopt_long(argc_, argv_, "hptusqrfv:c:alk:i:oyg:", kOptions, nullptr); int c = getopt_long(argc_, argv_, "hptusqrfv:c:alk:i:oyg:w:", kOptions, nullptr);
// End of the options // End of the options
if (c == -1) break; if (c == -1) break;
@@ -186,6 +188,7 @@ bool CommandlineOptions::Parse() {
.arg(tr("Other options"), tr("Display the on-screen-display"), .arg(tr("Other options"), tr("Display the on-screen-display"),
tr("Toggle visibility for the pretty on-screen-display"), tr("Toggle visibility for the pretty on-screen-display"),
tr("Change the language"), tr("Change the language"),
tr("Resize the window"),
tr("Equivalent to --log-levels *:1"), tr("Equivalent to --log-levels *:1"),
tr("Equivalent to --log-levels *:3"), tr("Equivalent to --log-levels *:3"),
tr("Comma separated list of class:level, level is 0-3")) tr("Comma separated list of class:level, level is 0-3"))
@@ -293,6 +296,11 @@ bool CommandlineOptions::Parse() {
if (!ok) play_track_at_ = -1; if (!ok) play_track_at_ = -1;
break; break;
case 'w':
window_size_ = QString(optarg);
player_action_ = Player_ResizeWindow;
break;
case '?': case '?':
default: default:
return false; return false;
@@ -370,7 +378,8 @@ QDataStream& operator<<(QDataStream &s, const CommandlineOptions &a) {
<< a.urls_ << a.urls_
<< a.log_levels_ << a.log_levels_
<< a.toggle_pretty_osd_ << a.toggle_pretty_osd_
<< a.playlist_name_; << a.playlist_name_
<< a.window_size_;
return s; return s;
@@ -380,6 +389,7 @@ QDataStream& operator>>(QDataStream &s, CommandlineOptions &a) {
quint32 player_action = 0; quint32 player_action = 0;
quint32 url_list_action = 0; quint32 url_list_action = 0;
s >> player_action s >> player_action
>> url_list_action >> url_list_action
>> a.set_volume_ >> a.set_volume_
@@ -391,7 +401,9 @@ QDataStream& operator>>(QDataStream &s, CommandlineOptions &a) {
>> a.urls_ >> a.urls_
>> a.log_levels_ >> a.log_levels_
>> a.toggle_pretty_osd_ >> a.toggle_pretty_osd_
>> a.playlist_name_; >> a.playlist_name_
>> a.window_size_;
a.player_action_ = CommandlineOptions::PlayerAction(player_action); a.player_action_ = CommandlineOptions::PlayerAction(player_action);
a.url_list_action_ = CommandlineOptions::UrlListAction(url_list_action); a.url_list_action_ = CommandlineOptions::UrlListAction(url_list_action);

View File

@@ -58,6 +58,7 @@ class CommandlineOptions {
Player_RestartOrPrevious = 7, Player_RestartOrPrevious = 7,
Player_StopAfterCurrent = 8, Player_StopAfterCurrent = 8,
Player_PlayPlaylist = 9, Player_PlayPlaylist = 9,
Player_ResizeWindow = 10
}; };
bool Parse(); bool Parse();
@@ -78,13 +79,13 @@ class CommandlineOptions {
QString language() const { return language_; } QString language() const { return language_; }
QString log_levels() const { return log_levels_; } QString log_levels() const { return log_levels_; }
QString playlist_name() const { return playlist_name_; } QString playlist_name() const { return playlist_name_; }
QString window_size() const { return window_size_; }
QByteArray Serialize() const; QByteArray Serialize() const;
void Load(const QByteArray &serialized); void Load(const QByteArray &serialized);
private: private:
// These are "invalid" characters to pass to getopt_long for options that // These are "invalid" characters to pass to getopt_long for options that shouldn't have a short (single character) option.
// shouldn't have a short (single character) option.
enum LongOptions { enum LongOptions {
VolumeUp = 256, VolumeUp = 256,
VolumeDown, VolumeDown,
@@ -120,6 +121,7 @@ class CommandlineOptions {
QString language_; QString language_;
QString log_levels_; QString log_levels_;
QString playlist_name_; QString playlist_name_;
QString window_size_;
QList<QUrl> urls_; QList<QUrl> urls_;
}; };

View File

@@ -31,6 +31,8 @@
#include <QApplication> #include <QApplication>
#include <QObject> #include <QObject>
#include <QWidget> #include <QWidget>
#include <QScreen>
#include <QWindow>
#include <QMetaObject> #include <QMetaObject>
#include <QThread> #include <QThread>
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
@@ -2312,8 +2314,46 @@ void MainWindow::CommandlineOptionsReceived(const CommandlineOptions &options) {
app_->player()->RestartOrPrevious(); app_->player()->RestartOrPrevious();
break; break;
case CommandlineOptions::Player_ResizeWindow:{
if (options.window_size().contains('x') && options.window_size().length() >= 4) {
QString str_w = options.window_size().left(options.window_size().indexOf('x'));
QString str_h = options.window_size().right(options.window_size().length() - options.window_size().indexOf('x') - 1);
bool w_ok = false;
bool h_ok = false;
int w = str_w.toInt(&w_ok);
int h = str_h.toInt(&h_ok);
if (w_ok && h_ok) {
QSize window_size(w, h);
if (window_size.isValid()) {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QScreen *screen = QWidget::screen();
#else
QScreen *screen = (window() && window()->windowHandle() ? window()->windowHandle()->screen() : nullptr);
#endif
if (screen) {
const QRect sr = screen->availableGeometry();
window_size = window_size.boundedTo(sr.size());
if (window_size.width() >= sr.width() && window_size.height() >= sr.height()) {
resize(window_size);
showMaximized();
}
else {
showNormal();
resize(window_size);
const QRect wr({}, size().boundedTo(sr.size()));
resize(wr.size());
move(sr.center() - wr.center());
}
}
}
}
}
break;
}
case CommandlineOptions::Player_None: case CommandlineOptions::Player_None:
break; break;
} }
if (!options.urls().empty()) { if (!options.urls().empty()) {