Exemplo n.º 1
0
int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QRect screenGeometry = QApplication::desktop()->screenGeometry();
  int ret = -1;
  char *buf;
  int window_x, window_y; // posX, posY
  int window_w, window_h; // width, height
  const char *window_font; // font family
  int window_fs, window_fw; // font size, weight
  int window_is; // icon size
  bool fs;

  // Processing command line arguments
  buf = getCmd(argv, argv + argc, "--help");
  if (buf)
  {
    cout << "Application selector usage:\n"
            "    app-selector [OPTION]... < [ITEM FILE]\n"
            "\nwhere options include:\n"
            "    --fullscreen                 use the entire screen.\n"
            "    --geometry WxH               size of window.\n"
            "    --font fontname              font family name.\n"
            "    --font-size size             font point size.\n"
            "    --font-weight weight         font weight.\n"
            "    --icon-size size             icon size.\n"
            "\nStructure of JSON file :\n"
            "[\n  {\n"
            "    \"name\": <string>, /* Friendly name of application       */\n"
            "    \"exec\": <string>, /* Command line or any data as output */\n"
            "    \"icon\": <string>  /* Icon path                          */\n"
            "  }, ...\n"
            "]\n";
    return 0;
  }
  buf = getCmdOption(argv, argv + argc, "--fullscreen");
  if (buf)
    fs = true;
  else
    fs = false;
  buf = getCmdOption(argv, argv + argc, "--geometry");
  if (!buf || sscanf(buf, "%ix%i", &window_w, &window_h) != 2)
  {
    window_x = 0;
    window_y = 0;
    window_w = screenGeometry.width();
    window_h = screenGeometry.height();
  }
  else
  {
      window_x = (screenGeometry.width() - window_w) / 2;
      window_y = (screenGeometry.height() - window_h) / 2;
  }
  buf = getCmdOption(argv, argv + argc, "--font");
  if (!buf)
    window_font = WINDOW_DFLT_FONT_FAMILY;
  else
    window_font = buf;
  buf = getCmdOption(argv, argv + argc, "--font-size");
  if (!buf || sscanf(buf, "%i", &window_fs) != 1)
  {
    window_fs = WINDOW_DFLT_FONT_POINT_SIZE;
  }
  buf = getCmdOption(argv, argv + argc, "--font-weight");
  if (!buf || sscanf(buf, "%i", &window_fw) != 1)
  {
    window_fw = WINDOW_DFLT_FONT_WEIGHT;
  }
  buf = getCmdOption(argv, argv + argc, "--icon-size");
  if (!buf || sscanf(buf, "%i", &window_is) != 1)
  {
    window_is = WINDOW_DFLT_ICON_SIZE;
  }

  vitem v;
  readJsonFromStream(&cin, v);

  MainWindow window;

  // Setup our window size and position
  window.resize(window_w, window_h);
  window.move(window_x, window_y);

  // Setup our font face, size and weight
  QFont font;
  font.setFamily(QString::fromUtf8(window_font));
  font.setPointSize(window_fs);
  font.setBold(false);
  font.setWeight(window_fw);
  window.setFont(font);

  // Setup our icon size
  window.setIconSize(QSize(window_is, window_is));

  for (vitem::iterator it = v.begin(); it != v.end(); ++it)
    window.AddItem(*it);

  if (fs)
    window.showFullScreen();
  else
    window.show();
  window.activateWindow();

  ret = a.exec();

  if (ret == 0)
  {
    int selection = window.GetSelected();
    if (selection >= 0)
    {
      Item item = v.at(selection);
      cout << item.execCmd.toStdString();
      cout << "\n";
    }
  }

  return ret;
}