summaryrefslogtreecommitdiffstats
path: root/kviewshell/history.cpp
blob: a876522037edae97f3cb50a4acbe4a8ca58e9e53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// history.cpp
//
// (C) 2001 Stefan Kebekus
// Distributed under the GPL

#include <config.h>

#include <kdebug.h>

#include "history.h"

HistoryItem::HistoryItem(Q_UINT32 p, Q_UINT32 y)
  : page(p), ypos(y)
{
}

bool HistoryItem::operator== (const HistoryItem& item) const
{
  return page == item.page && ypos == item.ypos;
}

History::History()
{
}

void History::add(Q_UINT32 page, Q_UINT32 ypos)
{
  HistoryItem item(page, ypos);

  if (historyList.empty())
  {
    currentItem = historyList.append(item);
  }
  else
  {
    // Don't add the same item several times in a row
    if (item == *currentItem)
      return;

      currentItem++;
    if (currentItem == historyList.end())
    {
      currentItem = historyList.append(item);
    }
    else
    {
      currentItem = historyList.insert(currentItem, item);
    }
    // Delete items starting after currentItem to the end of the list.
    QValueList<HistoryItem>::iterator deleteItemsStart = currentItem;
    deleteItemsStart++;
    historyList.erase(deleteItemsStart, historyList.end()); 

    if (historyList.size() > HISTORYLENGTH)
      historyList.pop_front();
  }
  emit backItem(currentItem != historyList.begin());
  emit forwardItem(false);
}

HistoryItem* History::forward()
{
  if (historyList.empty() || currentItem == historyList.fromLast())
    return 0;
    
    currentItem++;
  emit backItem(true);
  emit forwardItem(currentItem != historyList.fromLast());
  return &(*currentItem);
}

HistoryItem* History::back()
{
  if (historyList.empty() || currentItem == historyList.begin())
    return 0;
  
    currentItem--;
  emit backItem(currentItem != historyList.begin());
  emit forwardItem(true);
  return &(*currentItem);
}

void History::clear()
{
  historyList.clear();
  currentItem = historyList.begin();
  emit backItem(false);
  emit forwardItem(false);
}

#include "history.moc"