mydao.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "mydao.h"
  2. #include "api/color/colortable.h"
  3. #include <QCoreApplication>
  4. MyDao::MyDao(QObject *parent) : QObject(parent)
  5. {
  6. sqliteWrapper = new SqliteWrapper(this);
  7. #ifdef Q_OS_MAC
  8. sqliteWrapper->setFileName(QCoreApplication::applicationDirPath() + "/../../../data/data.db");
  9. #else
  10. sqliteWrapper->setFileName("data/data.db");
  11. #endif
  12. sqliteWrapper->setDbName("data");
  13. sqliteWrapper->open();
  14. }
  15. int MyDao::addNode(int pid, QString name, int sxh, QString msg)
  16. {
  17. QString sql = QString("insert into mind_data(pid,name,sxh,message) values (%1,'%2',%3,'%4')")
  18. .arg(pid)
  19. .arg(name)
  20. .arg(sxh)
  21. .arg(msg);
  22. sqliteWrapper->execute(sql);
  23. QSqlQuery qry;
  24. sql = "select max(id) from mind_data";
  25. sqliteWrapper->select(sql, qry);
  26. qry.next();
  27. return qry.value(0).toInt();
  28. }
  29. void MyDao::editNode(int id, QString name, QString msg)
  30. {
  31. QString sql;
  32. if (msg.isEmpty()) {
  33. sql = QString("update mind_data set name='%1' where id=%2").arg(name).arg(id);
  34. } else {
  35. sql = QString("update mind_data set name='%1', message='%2' where id=%3").arg(name).arg(msg).arg(id);
  36. }
  37. sqliteWrapper->execute(sql);
  38. }
  39. void MyDao::initGlobalSet()
  40. {
  41. QSqlQuery qry;
  42. QString sql;
  43. sql = "select * from mind_global";
  44. sqliteWrapper->select(sql, qry);
  45. if (!qry.next()) {
  46. sql = QString("insert into mind_global(fontcolorindex,backcolorindex) values (%1,%2)")
  47. .arg(0)
  48. .arg(ColorTable::lastIndex());
  49. sqliteWrapper->execute(sql);
  50. }
  51. sql = "select * from mind_global";
  52. sqliteWrapper->select(sql, qry);
  53. if (qry.next()) {
  54. ColorTable::fontColorIndex = qry.value("fontcolorindex").toInt();
  55. ColorTable::backColorIndex = qry.value("backcolorindex").toInt();
  56. }
  57. //获取颜色
  58. sql = "select * from mind_color order by id desc";
  59. sqliteWrapper->select(sql, qry);
  60. int i = 0;
  61. while (qry.next()) {
  62. if (!ColorTable::colorIndexes.contains(qry.value("colorindex").toInt())) {
  63. ColorTable::colorIndexes.push(qry.value("colorindex").toInt());
  64. i++;
  65. if (i > 10) {
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. void MyDao::saveGlobalSet(QString fldName, int value)
  72. {
  73. QString sql = QString("update mind_global set %1=%2").arg(fldName).arg(value);
  74. sqliteWrapper->execute(sql);
  75. }
  76. void MyDao::addPix(int id, QPixmap &pix)
  77. {
  78. QString sql = QString("update mind_data set img=? where id=%1").arg(id);
  79. QSqlQuery qry;
  80. sqliteWrapper->prepare(sql, qry);
  81. QByteArray ba;
  82. QBuffer buf(&ba);
  83. QImage img = pix.toImage();
  84. img.save(&buf, "png");
  85. qry.bindValue(0, ba);
  86. qry.exec();
  87. }