Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcontenthistory.h"
Toshihiro Shimizu 890ddd
#include <qstring></qstring>
Toshihiro Shimizu 890ddd
#include <qdatetime></qdatetime>
Toshihiro Shimizu 890ddd
#include <qstringlist></qstringlist>
Toshihiro Shimizu 890ddd
#include <qprocess></qprocess>
Toshihiro Shimizu 890ddd
#include <qtextstream></qtextstream>
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
e280ae
#include <windows.h></windows.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#include <map></map>
Toshihiro Shimizu 890ddd
#include <qfile></qfile>
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
class HistoryRec
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
QDateTime m_date;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
HistoryRec(const QDateTime&date QString&user, const QString&machine)
Toshihiro Shimizu 890ddd
 : date(date)
Toshihiro Shimizu 890ddd
 , m_user
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TContentHistory::TContentHistory(bool isLevel)
Shinya Kitaoka 120a6e
    : m_isLevel(isLevel), m_frozenHistory() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TContentHistory::~TContentHistory() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TContentHistory *TContentHistory::clone() const {
Shinya Kitaoka 120a6e
  TContentHistory *history = new TContentHistory(m_isLevel);
Shinya Kitaoka 120a6e
  history->deserialize(serialize());
Shinya Kitaoka 120a6e
  return history;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TContentHistory::deserialize(const QString ¤tHistory) {
Shinya Kitaoka 120a6e
  m_frozenHistory = currentHistory;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
QStringList strlist = str.split("#");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//skip strlist[0], is the header
Toshihiro Shimizu 890ddd
int i;
Toshihiro Shimizu 890ddd
for (i=1; i
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
  QString str = strlist.at(i);
Toshihiro Shimizu 890ddd
  parseLine(&str);
Toshihiro Shimizu 890ddd
   }
Toshihiro Shimizu 890ddd
}*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline QString blanks(const QString &str, int count = 15) {
Shinya Kitaoka 120a6e
  QString res = str;
Shinya Kitaoka 120a6e
  if (res.size() >= count)
Shinya Kitaoka 120a6e
    res.truncate(count);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    res += QString(count - str.size(), ' ');
Shinya Kitaoka 120a6e
  return res;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline QString getStr(const TFrameId &id) {
Shinya Kitaoka 120a6e
  if (id.getLetter() != 0)
Shinya Kitaoka 120a6e
    return QString::number(id.getNumber()) + id.getLetter();
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return QString::number(id.getNumber());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const QString Fmt = "dd MMM yy   hh:mm";
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static QString getLine(int counter, const QDateTime &date,
shun-iwasawa 27b0cf
                       const set<tframeid> &frames) {</tframeid>
Shinya Kitaoka 120a6e
  static QString user;
Shinya Kitaoka 120a6e
  static QString machine;
Shinya Kitaoka 120a6e
  if (user == "") {
Shinya Kitaoka 120a6e
    QStringList list = QProcess::systemEnvironment();
Shinya Kitaoka 120a6e
    int j;
Shinya Kitaoka 120a6e
    for (j = 0; j < list.size(); j++) {
Shinya Kitaoka 120a6e
      QString value = list.at(j);
Shinya Kitaoka 120a6e
      if (value.startsWith("USERNAME="))
Shinya Kitaoka 120a6e
        user = blanks(value.right(value.size() - 9));
Shinya Kitaoka 120a6e
      else if (value.startsWith("COMPUTERNAME="))
Shinya Kitaoka 120a6e
        machine = blanks(value.right(value.size() - 13));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (frames.empty())
Shinya Kitaoka 120a6e
    return "| #" + blanks(QString::number(counter), 4) +
Shinya Kitaoka 120a6e
           blanks(date.toString(Fmt), 20) + blanks(machine, 12) + user + " |";
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QString framesStr;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  set<tframeid>::const_iterator it = frames.begin();</tframeid>
Shinya Kitaoka 120a6e
  TFrameId first, last;
Shinya Kitaoka 120a6e
  while (it != frames.end()) {
Shinya Kitaoka 120a6e
    first = last = *it;
Shinya Kitaoka 120a6e
    ++it;
Shinya Kitaoka 120a6e
    while (it != frames.end() &&
Shinya Kitaoka 120a6e
           ((*it).getNumber() == last.getNumber() ||  // 1a, 1b....
Shinya Kitaoka 120a6e
            (*it).getNumber() == last.getNumber() + 1)) {
Shinya Kitaoka 120a6e
      assert(*it > last);
Shinya Kitaoka 120a6e
      last = *it, ++it;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    framesStr += getStr(first) + ((first != last) ? "-" + getStr(last) : "");
Shinya Kitaoka 120a6e
    if (it != frames.end()) framesStr += ", ";
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return "| #" + blanks(QString::number(counter), 4) +
Shinya Kitaoka 120a6e
         blanks(date.toString(Fmt), 20) + blanks(machine, 12) + user +
Shinya Kitaoka 120a6e
         blanks(framesStr, 20) + " |";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static int getCurrentCount(const QString &str) {
Shinya Kitaoka 120a6e
  if (str == "") return 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int from = str.lastIndexOf('#') + 1;
Shinya Kitaoka 120a6e
  assert(from != -1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int to = str.indexOf(" ", from) - 1;
Shinya Kitaoka 120a6e
  assert(to != -1);
Shinya Kitaoka 120a6e
  assert(from <= to);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return (str.mid(from, to - from + 1)).toInt();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const QString TContentHistory::currentToString() const {
Shinya Kitaoka 120a6e
  if (m_records.empty()) return "";
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int counter = getCurrentCount(m_frozenHistory);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!m_isLevel) {
Shinya Kitaoka 120a6e
    assert(m_records.size() == 1);
Shinya Kitaoka 120a6e
    return getLine(++counter, m_records.begin()->second, set<tframeid>());</tframeid>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QString out;
Shinya Kitaoka 120a6e
  std::multimap<qdatetime, tframeid=""> dateSorted;</qdatetime,>
Shinya Kitaoka 120a6e
  std::map<tframeid, qdatetime="">::const_iterator it;</tframeid,>
Shinya Kitaoka 120a6e
  for (it = m_records.begin(); it != m_records.end(); ++it)
Shinya Kitaoka 120a6e
    dateSorted.insert(pair<qdatetime, tframeid="">(it->second, it->first));</qdatetime,>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::multimap<qdatetime, tframeid="">::const_iterator it1 = dateSorted.begin();</qdatetime,>
Shinya Kitaoka 120a6e
  QDateTime currDate = it1->first;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (it1 != dateSorted.end()) {
Shinya Kitaoka 120a6e
    set<tframeid> frames;</tframeid>
Shinya Kitaoka 120a6e
    while (it1 != dateSorted.end() && currDate == it1->first) {
Shinya Kitaoka 120a6e
      frames.insert(it1->second);
Shinya Kitaoka 120a6e
      ++it1;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    assert(!frames.empty());
Shinya Kitaoka 120a6e
    out += getLine(++counter, currDate, frames);
Shinya Kitaoka 120a6e
    if (it1 != dateSorted.end()) currDate = it1->first;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return out;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const QString TContentHistory::serialize() const {
Shinya Kitaoka 120a6e
  const QString currentHistory = currentToString();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_frozenHistory != "")
Shinya Kitaoka 120a6e
    return m_frozenHistory + currentHistory;
Shinya Kitaoka 120a6e
  else if (currentHistory != "") {
Shinya Kitaoka 120a6e
    if (m_isLevel)
Shinya Kitaoka 120a6e
      return "| #    DATE:       Time:   MACHINE:    USER:          FRAMES "
Shinya Kitaoka 120a6e
             "MODIFIED:     |" +
Shinya Kitaoka 120a6e
             currentHistory;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      return "| #    DATE:       Time:   MACHINE:    USER:           |" +
Shinya Kitaoka 120a6e
             currentHistory;
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    return "";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TContentHistory::fixCurrentHistory() {
Shinya Kitaoka 120a6e
  m_frozenHistory = serialize();
Shinya Kitaoka 120a6e
  m_records.clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TContentHistory::frameRangeModifiedNow(const TFrameId &fromId,
Shinya Kitaoka 120a6e
                                            const TFrameId &toId) {
Shinya Kitaoka 120a6e
  assert(m_isLevel);
Shinya Kitaoka 120a6e
  QDateTime date = QDateTime::currentDateTime();
Shinya Kitaoka 120a6e
  QDateTime dateNoSecs(date.date(), date.time().addSecs(-date.time().second()));
Shinya Kitaoka 120a6e
  assert(dateNoSecs.secsTo(date) == date.time().second());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  m_records[fromId] = dateNoSecs;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (fromId == toId) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (i = fromId.getNumber() + 1; i <= toId.getNumber() - 1; i++)
Shinya Kitaoka 120a6e
    m_records[TFrameId(i)] = dateNoSecs;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_records[toId] = dateNoSecs;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TContentHistory::modifiedNow() {
Shinya Kitaoka 120a6e
  assert(!m_isLevel);
Shinya Kitaoka 120a6e
  QDateTime date = QDateTime::currentDateTime();
Shinya Kitaoka 120a6e
  QDateTime dateNoSecs(date.date(), date.time().addSecs(-date.time().second()));
Shinya Kitaoka 120a6e
  assert(dateNoSecs.secsTo(date) == date.time().second());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_records[0] = dateNoSecs;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
void testHistory()
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
                {
Shinya Kitaoka 120a6e
                TContentHistory ch(true);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
                ch.frameModifiedNow(TFrameId(13));
Shinya Kitaoka 120a6e
                Sleep(1000);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
                ch.frameModifiedNow(TFrameId(3, 'c'));
Shinya Kitaoka 120a6e
                ch.frameRangeModifiedNow(TFrameId(5), TFrameId(7));
Shinya Kitaoka 120a6e
                ch.fixCurrentHistory();
Shinya Kitaoka 120a6e
                Sleep(2000);
Shinya Kitaoka 120a6e
                ch.frameRangeModifiedNow(TFrameId(6), TFrameId(9));
Shinya Kitaoka 120a6e
                ch.frameModifiedNow(TFrameId(11));
Shinya Kitaoka 120a6e
                QString str1 = ch.serialize();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
                TContentHistory ch1(true);
Shinya Kitaoka 120a6e
                ch1.deserialize(str1);
Shinya Kitaoka 120a6e
                ch1.frameRangeModifiedNow(TFrameId(2), TFrameId(3, 'c'));
Shinya Kitaoka 120a6e
                Sleep(2000);
Shinya Kitaoka 120a6e
                ch1.frameModifiedNow(TFrameId(3, 'a'));
Shinya Kitaoka 120a6e
                ch1.fixCurrentHistory();
Shinya Kitaoka 120a6e
                Sleep(2000);
Shinya Kitaoka 120a6e
                ch1.frameModifiedNow(TFrameId(11, 'b'));
Shinya Kitaoka 120a6e
                ch1.frameModifiedNow(TFrameId(12));
Shinya Kitaoka 120a6e
                QString str2 = ch1.serialize();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
                QFile f("C:\\temp\\out.txt");
Shinya Kitaoka 120a6e
                f.open(QIODevice::WriteOnly | QIODevice::Text);
Shinya Kitaoka 120a6e
                QTextStream out(&f);
Shinya Kitaoka 120a6e
                out << str2;
Shinya Kitaoka 120a6e
                }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    {
Shinya Kitaoka 120a6e
                TContentHistory ch(false);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
                ch.modifiedNow();
Shinya Kitaoka 120a6e
                Sleep(1000);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
                ch.modifiedNow();
Shinya Kitaoka 120a6e
                ch.modifiedNow();
Shinya Kitaoka 120a6e
                ch.fixCurrentHistory();
Shinya Kitaoka 120a6e
                Sleep(2000);
Shinya Kitaoka 120a6e
                ch.modifiedNow();
Shinya Kitaoka 120a6e
                ch.modifiedNow();
Shinya Kitaoka 120a6e
                QString str1 = ch.serialize();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
                TContentHistory ch1(false);
Shinya Kitaoka 120a6e
                ch1.deserialize(str1);
Shinya Kitaoka 120a6e
                ch1.modifiedNow();
Shinya Kitaoka 120a6e
                Sleep(2000);
Shinya Kitaoka 120a6e
                ch1.modifiedNow();
Shinya Kitaoka 120a6e
                ch1.fixCurrentHistory();
Shinya Kitaoka 120a6e
                Sleep(2000);
Shinya Kitaoka 120a6e
                ch1.modifiedNow();
Shinya Kitaoka 120a6e
                ch1.modifiedNow();
Shinya Kitaoka 120a6e
                QString str2 = ch1.serialize();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
                QFile f("C:\\temp\\out1.txt");
Shinya Kitaoka 120a6e
                f.open(QIODevice::WriteOnly | QIODevice::Text);
Shinya Kitaoka 120a6e
                QTextStream out(&f);
Shinya Kitaoka 120a6e
                out << str2;
Shinya Kitaoka 120a6e
                }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/